小编Vij*_*han的帖子

防止多行TextView不必要地扩展到它的最大宽度

扩展为多行的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的宽度(不重新格式化内容),但是必须根据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)

带有边距的TextView显示

android

8
推荐指数
2
解决办法
1753
查看次数

标签 统计

android ×1