Android多行文本换行



我正在构建我的第一个Android应用程序的过程中,遇到了一个奇怪的问题,文本换行似乎不像人们期望的那样工作。XML布局是RelativeLayout,因为它是RecyclerView的列表项模板。在行模板里面有三个TextView对象-意图是前两个在一行上,第三个在单独的一行上。下面是XML结构:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- should be ~85% of view -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_margin="6dp"
android:background="@drawable/list_item_border"
android:padding="6dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- this text should wrap when the content is too big, but it
only does if I set a maxWidth which feels hacky since that
isn't a great multi-device solution -->

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txDisplayName"
tools:text="** I should wrap when I hit the max available space within my container **"
android:textStyle="bold"
android:elegantTextHeight="true"
android:singleLine="false"
android:minLines="1"
android:maxLines="99"
android:lines="3"
android:textSize="16sp" />
<!-- secondary item on this "row" - should always be below the TextView above -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/txSKU"
android:textStyle="italic"
android:textSize="18dp"
tools:text="00000-0000-00" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:minWidth="30sp"
android:gravity="right">
<!-- should be ~15% of total view -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:textSize="18dp"
android:id="@+id/tvReceivingSummary"
tools:text="00/00" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>

作为参考,@drawable/list_item_border只是在整个行周围添加了一个小边框。它被定义为:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="2dp"
android:color="@color/prx_blue" />
<corners android:radius="2dp" />
</shape>

最终目标是这样的(全屏宽度):

==================================================================
| Potentially long text that should always wrap when |     00/00 |
| the text gets too long. The values come from an    |           |
| API so it's not controllable text (meaning I can't |           |
| easily add "n" because I wouldn't know where to   |           |
| separate it out.                                   |           |
==================================================================
| This is the second row - no need to wrap                       |
==================================================================

我尝试了RelativeLayout,ConstraintLayoutTableLayout的各种组合,但似乎我可以得到文本包装(但重叠本身),或者我可以得到它没有重叠,但它不换行,并将00/00推离视图,所以它不可见。

有什么建议吗?我知道我必须接近并看过多个SO帖子,但作为新手,我在这一点上有点旋转我的轮子,希望有人可以帮助。

您熟悉android:layout_weight吗?这是LinearLayout的子属性,似乎非常适合你的情况

仅使用android:layout_weight="85"android:layout_weight="15"为您的子线性,layout_width="0"为两者(作为父LinearLayoutorientation="horizontal"将根据权重拉伸它们)

顺便说一句。还有相关的参数weightSum

顺便说一句。你的根RelativeLayoutwrap_content的尺寸和一个和唯一的子- "main"LinearLayout-有match_parent。这对绘画来说效率很低……特别是因为你的RelativeLayout根本不需要…移除它,在此之前将xmlns属性移至"main";LinearLayout和设置layout_height="wrap_content",以便RecyclerView更好地拟合和测量。一般:嵌套较少的xml/层=>更好的性能

最新更新