Android: Last line of textview cut off

后端 未结 20 1141
北荒
北荒 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:05

    I've encountered the same cut-off issue as shown at the screenshot. It is caused by the baseline alignment in the horizontal LinearLayout. TextView and Spinner have different baselines due to font size difference. To fix the issue it is needed to disable baseline alignment for the layout by setting:

    android:baselineAligned="false"
    

    or in the code:

    layout.setBaselineAligned(false);
    
    0 讨论(0)
  • 2020-11-28 03:05

    I had the same problem, and found that simply adding

    android:includeFontPadding="false"
    

    the final line of text no longer had its descenders clipped.

    0 讨论(0)
  • 2020-11-28 03:05

    I found a different solution by extending TextView and adding a custom Class like this:

     public class AdaptingTextView extends TextView {
    
        public AdaptingTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        public AdaptingTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public AdaptingTextView(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);
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-11-28 03:06

    If you have this problem and your TextView is inside a RelativeLayout, try switching the RelativeLayout for a LinearLayout.

    That fixed the problem for me

    0 讨论(0)
  • 2020-11-28 03:09

    My solution was close to the accepted one, but I had to change it to

       android:layout_gravity="fill_vertical"
    

    instead. Otherwise the other rows would have been stretch as well with added line breaks at random places. For example, the biggest row had 4 lines, so another row was changed from

    this is a testphrase

    to

    thi
    s is
    a testph
    rase
    
    0 讨论(0)
  • 2020-11-28 03:09

    getViewTreeObserver().addOnGlobalLayoutListener does not work in a recycler view. If you're using a recycler, use View.addOnLayoutChangeListener:

    I found that the ellipsizing I defined for textView in xml was not always reflected so I programmatically set it before reassigning the text property. This worked for me.

    textView.addOnLayoutChangeListener(new OnLayoutChangeListener() {
        @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom,
                                       int oldLeft, int oldTop, int oldRight, int oldBottom) {
                textView.removeOnLayoutChangeListener(this);
                float lineHeight = textView.getLineHeight();
                int maxLines = (int) (textView.getHeight() / lineHeight);
                if (textView.getLineCount() != maxLines) {
                    textView.setLines(maxLines);
                    textView.setEllipsize(TextUtils.TruncateAt.END);
                    // Re-assign text to ensure ellipsize is performed correctly.
                    textView.setText(model.getText());
                }
            }
        });
    
    0 讨论(0)
提交回复
热议问题