如果你有一个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)
扩展为多行的TextView似乎会自动扩展以适应其最大可能宽度.我怎样才能防止这种情况发生?
这是一个示例布局(test.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<TextView
android:background="@color/green"
android:layout_alignParentRight="true"
android:id="@+id/foo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="This is the message a much shorter message"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/black" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

如果我在左边添加一个边距,它会正确缩小TextView的宽度(不重新格式化内容),但是必须根据TextView的内容计算边距的大小.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<TextView
android:layout_marginLeft="32dp"
android:background="@color/green"
android:layout_alignParentRight="true"
android:id="@+id/foo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="This is the message a much shorter message"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/black" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我有这样的布局(我删除了一些属性,因为它们真的没关系,完整的演示项目在这里):
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content" (* this is important)
android:layout_height="wrap_content"
android:breakStrategy="balanced" (* this is important)
android:text="@string/long_text" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
文本long_text很长,所以它会分开几行.
两条重要的路线是android:layout_width="wrap_content"和android:breakStrategy="balanced".
我希望TextView根据实际文本宽度计算宽度,但不会.
有人知道如何解决这个问题吗?
UPDATE
属性android:breakStrategy="balanced"仅适用于API 23更高版本.在我的示例中,文本占用3行,balanced策略使每行大致相同的宽度.
我希望在这种情况下,视图宽度本身将与最长线相同.
我正在寻找任何解决方法.我需要像环聊聊天一样的解决方案.我无法弄清楚它是如何工作的.
更新2
不幸的是,接受的答案不适用于RTL文本.
我需要渲染任意长度的引用块.文本必须与左边对齐,而块本身与右边对齐,类似于这个:

对于我正在尝试一种TextView用android:width="wrap_content",android:gravity="start"和android:layout_gravity="end".但是,只有当文本适合单行时,这才能正常工作 - 如果文本长TextView于此行,则行为如下:
Raw persistence may be the only option other than giving up entirely.- 仍然是块的行为match_parent.这是布局(paddingRight替换layout_marginRight为突出目的 - 行为是相同的两种方式):
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:lineSpacingExtra="3.5dp"
android:paddingLeft="24dp"
android:layout_marginRight="24dp"
android:text="Raw persistence may be the only option other than giving up entirely."
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:lineSpacingExtra="3.5dp"
android:paddingLeft="24dp"
android:layout_marginRight="24dp"
android:text="Raw persistence may be the only option other than giving up entirely."
/> …Run Code Online (Sandbox Code Playgroud)