为什么多行时TextView带有尾部填充? [英] Why does TextView have end padding when multi line?

查看:83
本文介绍了为什么多行时TextView带有尾部填充?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

If you have a TextView with layout_width="wrap_content" and it has to wrap to a second line to contain the text, then it will size its width to use up all of the space available (respecting margins etc). But why is there padding at the end of the view? I just told it to wrap_content, so it should wrap that content! This seems like a bug, this is visible in the chat UI of the stock Messenger app. (The image is from my own app though. But that extra space is definitely not in the 9 patch.)

有什么解决方法吗?

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

Update: Responders/commenters missed the point. Maybe the image I uploaded was misleading because it was styled from my app. The problem occurs with any TextView, you can see by styling the background that the view bounds will no longer be tight. I uploaded a different 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"
    />

推荐答案

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

At first, when seeing your post, i thought that the problem was because standard Android TextView have some default padding defined in their base style. If one wants to remove it, ones can try it something like :

android:paddingEnd="0dp"

android:paddingRight="0dp"

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

As your post has been updated, I understand that your problem does not comes from padding, but from word wrapping. Indeed, when there are several lines to display, Android TextView use the whole available space in width.

在此帖子中所述,并非标准解决方案,您需要自定义文本视图以在填充后固定其宽度.

As stated in this post, there is no standard solution for this and you will need to customize your text view to fix its width after filling it.

覆盖textView的onMeasure方法,如下所示(应来自天空"答案):

Overriding onMeasure method of your textView like below shoud work (inspired from "sky" answer) :

@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);
            }
        }
    }
}

这篇关于为什么多行时TextView带有尾部填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆