Android Eclipse中的线性布局有多个文本视图未自动换行。

4
我有一个水平线性布局,其中包含多个文本视图。布局的宽度为fill_parent,高度为wrap_content。我的问题是,当文本视图的长度超过设备屏幕的允许宽度时,它会将最后一个文本视图挤到右侧,而不是将其换行(这是我想要的结果)。我认为将高度设置为wrap_content可以解决这个问题。
我该怎么做才能使我的线性布局中的多个文本视图在超出父元素宽度时自动换行呢?
以下是我的布局代码片段:
<LinearLayout
                                android:id="@+id/linearLayout20"
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
                                android:paddingTop="3dip" >

                                <TextView android:layout_height="wrap_content" android:textColor="@color/black" android:text="Car Info:  " android:id="@+id/textView023" android:layout_width="wrap_content" android:textStyle="bold" android:textSize="@dimen/item_main_text_size"></TextView>
                                <TextView android:layout_height="wrap_content" android:textColor="@color/black" android:text="2005" android:id="@+id/autorental_item_caryear" android:layout_width="wrap_content" android:textSize="@dimen/item_main_text_size"></TextView>
                                <TextView android:layout_height="wrap_content" android:textColor="@color/black" android:paddingLeft="3dip" android:text="Chevy" android:id="@+id/autorental_item_carmake" android:layout_width="wrap_content" android:textSize="@dimen/item_main_text_size"></TextView>
                                <TextView android:layout_height="wrap_content" android:textColor="@color/black" android:paddingLeft="3dip" android:text="Rio" android:id="@+id/autorental_item_carmodel" android:layout_width="wrap_content" android:textSize="@dimen/item_main_text_size"></TextView>
                                <TextView android:layout_height="wrap_content" android:textColor="@color/black" android:paddingLeft="3dip" android:text="-" android:id="@+id/textView2" android:paddingRight="3dip" android:layout_width="wrap_content" android:textSize="@dimen/item_main_text_size"></TextView>
                                <TextView android:layout_height="wrap_content" android:textColor="@color/black" android:paddingLeft="3dip" android:text="Red" android:id="@+id/autorental_item_carcolor" android:layout_width="wrap_content" android:textSize="@dimen/item_main_text_size"></TextView>
                            </LinearLayout>

使用长文本视图时,它会呈现如下内容:
Car Info:  2002 Chevrolet Silverado - Pu
                                     rp
                                     le 

我希望您能够将其设计为这样:

Car Info:  2002 Chevrolet Silverado - 
Purple

你想要一个宽度相等的TextView吗? - Paresh Mayani
不需要它们大小相等。它们将根据稍后输入的内容而变化。我只希望如果文本视图中的文本变得太长而无法适合一行,它将换到下一行。现在,如果文本太长,它会将最后一个文本视图压缩到右侧,使其成为多行,但每行只有1-2个字符全部压缩到右侧。 - Nick
2个回答

3

使用包含多个TextView(或其他内容,如按钮等)的LinearLayout是无法实现您所期望的结果的:

每个水平/垂直线性布局都为其子视图提供了水平/垂直边界,每个子视图可以在其中进行自己的操作。因此,每个视图只能沿着未被限制的方向扩展,对于您的情况是垂直方向。

以下示例说明了问题。外部短横线代表屏幕边界。

| First Text- | Short text | Long text will take all the space possible | T |
| View with   |            |                                            | e |
| additional  |            |                                            | x |
| given       |            |                                            | t |
| maximum     |            |                                            |   |
| width. Long |            |                                            |   |
| text makes  |            |                                            |   |
| it expand   |            |                                            |   | 
| down        |            |                                            |   |

你可以把所有的文本放入一个文本视图中来解决它。但是如果你想制作一个漂亮的列表,那么你需要再次格式化放入其中的字符串。

1
那很有道理。谢谢您清晰简明的回答。 - Nick

1

使用LinearLayout无法实现该结果。我认为在这种情况下最好的解决方案是使用单个TextView,并使用多种样式(如果需要文本的动态值)。请参考此处以获取更多见解: 是否可以在TextView中使用多种样式?

如果您有静态文本,则可以在strings.xml中将符合html规范的值包含在CDATA块中,并直接在布局中使用它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接