为什么TextView在多行时有结束填充?

and*_*guy 12 android textview

如果你有一个TextView,layout_width="wrap_content"它必须包装到第二行以包含文本,那么它将调整其宽度以用尽所有可用空间(尊重边距等).但为什么在视图的末尾有填充?我只是告诉它wrap_content,所以它应该包装内容!这似乎是一个错误,这在股票Messenger应用程序的聊天UI中可见.(图像来自我自己的应用程序.但是额外的空间肯定不在 9补丁中.)

任何解决方法?

更新:响应者/评论者错过了这一点.也许我上传的图片具有误导性,因为它是从我的应用程序设计的.任何TextView都会出现问题,您可以通过样式化视图边界将不再紧密的背景来查看.我上传了一张不同的图片.以下是图像中TextView的XML:

        <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"
    />
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Yve*_*erm 5

最初,看到您的帖子时,我认为问题是因为标准Android TextView在其基本样式中定义了一些默认填充。如果要删除它,可以尝试如下操作:

android:paddingEnd="0dp"
Run Code Online (Sandbox Code Playgroud)

要么

android:paddingRight="0dp"
Run Code Online (Sandbox Code Playgroud)

由于您的帖子已更新,因此我了解到您的问题不是出自填充,而是来自自动换行。确实,当要显示几行时,Android TextView会使用整个可用空间的宽度。

如本文所述没有标准的解决方案,您需要自定义文本视图以在填充后固定其宽度。

像下面的工作一样覆盖textView的onMeasure方法(灵感来自“天空”答案):

@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);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


hof*_*are 5

我发现选择的答案是有帮助的,尽管它并不能完全解决填充问题。我将选定的答案与本文的答案结合起来,得出了一个适用于填充的视图。仅供参考,其他帖子的答案有一个缺陷,即视图的末端有时会被截断几个像素。

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;
  }
}
Run Code Online (Sandbox Code Playgroud)