Android: Last line of textview cut off

后端 未结 20 1376
北荒
北荒 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: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());
                }
            }
        });
    

提交回复
热议问题