Why does TextView have end-of-fill when multi-line?

If you have a TextView with layout_width="wrap_content", and it needs to wrap to the second line to contain the text, then it will determine the width to use all available space (including fields, etc.). But why are there indents at the end of the submission? I just said wrap_content about it, so it should wrap this content! This seems like an error, it is visible in the chat user interface of the Messenger application. (Image from my own application, though, but this extra space is definitely not included in 9 patches.)

Any workaround?

Update: defendants / commentators missed the point. Perhaps the image I uploaded is misleading because it was created from my application. The problem arises with any TextView, you can see, creating a background that the viewing borders will no longer be rigid. I uploaded another image. Here is the XML for the TextViews in the image:

        <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="20dp"
    android:layout_marginStart="20dp"
    android:background="#dddddd"
    android:text="This doesn't wrap"
    android:layout_marginTop="20dp"
    android:layout_marginBottom="20dp"
    android:layout_gravity="center_horizontal"
    />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="20dp"
    android:layout_marginStart="20dp"
    android:layout_gravity="center_horizontal"
    android:background="#dddddd"
    android:text="This wraps and look, the bounds does not fit tight against the right edge of text"
    />

enter image description here

+4
source share
3 answers

At first, when I saw the message, I thought that the problem is that the standard Android TextView has a default add-on defined in their basic style. If someone wants to remove it, you can try something like:

android:paddingEnd="0dp"

or

android:paddingRight="0dp"

, , - , . , , Android TextView .

, , , .

onMeasure textView, shoud ( "" ):

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int specModeW = MeasureSpec.getMode(widthMeasureSpec);
    if (specModeW != MeasureSpec.EXACTLY) {
        Layout layout = getLayout();
        int linesCount = layout.getLineCount();
        if (linesCount > 1) {
            float textRealMaxWidth = 0;
            for (int n = 0; n < linesCount; ++n) {
                textRealMaxWidth = Math.max(textRealMaxWidth, layout.getLineWidth(n));
            }
            int w = (int) Math.ceil(textRealMaxWidth);
            if (w < getMeasuredWidth()) {
                super.onMeasure(MeasureSpec.makeMeasureSpec(w, MeasureSpec.AT_MOST),
                        heightMeasureSpec);
            }
        }
    }
}
+1

, . , , . FYI, , .

public class TightTextView extends TextView {
  public TightTextView(Context context) {
    super(context);
  }

  public TightTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public TightTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int specModeW = MeasureSpec.getMode(widthMeasureSpec);
    if (specModeW != MeasureSpec.EXACTLY) {
      Layout layout = getLayout();
      if (layout != null) {
        int w = (int) Math.ceil(getMaxLineWidth(layout)) + getCompoundPaddingLeft() + getCompoundPaddingRight();
        if (w < getMeasuredWidth()) {
          super.onMeasure(MeasureSpec.makeMeasureSpec(w, MeasureSpec.AT_MOST),
          heightMeasureSpec);
        }
      }
    }
  }

  private float getMaxLineWidth(Layout layout) {
    float max_width = 0.0f;
    int lines = layout.getLineCount();
    for (int i = 0; i < lines; i++) {
      if (layout.getLineWidth(i) > max_width) {
        max_width = layout.getLineWidth(i);
      }
    }
    return max_width;
  }
}
+1

, , "" "fit" . , | .

0

Source: https://habr.com/ru/post/1661918/


All Articles