Android: Last line of textview cut off

后端 未结 20 1336
北荒
北荒 2020-11-28 02:46

I have a horizontal LinearLayout containing a TextView followed by a Spinner next to it. This LinearLayout is dynamically

20条回答
  •  情歌与酒
    2020-11-28 03:30

    For me, this solution worked like a charm.

    The height and width of my outermost layout was set dynamically, so the TextView contained within got it's text cut even if I set android:maxLines in my xml (for different devices it was behaving differently).

    After trying out different methods, finally I got a solution that fixed my issue.

    Textview:

    public class CustomTextView extends androidx.appcompat.widget.AppCompatTextView {
        public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
        public CustomTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
        public CustomTextView(Context context) {
            super(context);
        }
        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
            // set fitting lines to prevent cut text
            int fittingLines = h / this.getLineHeight();
            if (fittingLines > 0) {
                this.setLines(fittingLines);
                this.setEllipsize(TextUtils.TruncateAt.END);
            }
        }
    }
    

    xml:

    
    

提交回复
热议问题