具有多个 TextView 的Android Eclipse线性布局不换行

标签 android eclipse textview android-linearlayout

我有一个水平线性布局,其中包含多个 TextView 。布局的宽度为 fill_parent,高度为 wrap_content。我的问题是,当 TextView 长于我的设备允许的屏幕宽度时,它会将最后一个 TextView 向右挤压,而不是换行(这是我想要的结果)。我认为 wrap_content 的高度可以解决这个问题。

我该怎么做才能使具有多个 TextView 的线性布局在 TextView 超过父 View 的宽度时将其换行?

这是我的布局的一个 fragment :

<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>

对于长 TextView ,它会呈现如下内容:

Car Info:  2002 Chevrolet Silverado - Pu
                                     rp
                                     le 

我希望它是这样的:

Car Info:  2002 Chevrolet Silverado - 
Purple

最佳答案

包含多个 TextView(或其他东西,如按钮等)的 LinearLayout 无法获得您想要的结果:

每个水平/垂直线性布局为其 subview 提供水平/垂直边界,每个 subview 都可以在其中做自己的事情。所以每个 View 只能在没有边界的方向上扩展,在你的例子中是垂直的。

这是一个说明问题的示例。外面的破折号代表屏幕边框。

| 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        |            |                                            |   |

您可以通过将所有文本放入单个 TextView 来解决它。然后,如果您想制作一个漂亮的列表,您需要再次格式化放入其中的字符串。

关于具有多个 TextView 的Android Eclipse线性布局不换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10174631/

相关文章:

eclipse - 是否可以扩展 Eclipse 搜索菜单

android - RelativeLayout TextView 覆盖

android - 在 Android 的 textview 中格式化文本项目符号的最简单方法

android - 按时间间隔播放声音的最佳方式?

java - 如何调试 Android 后台服务?

java - 在 Eclipse 中调试时重新加载 java 类

c++ - 在 Eclipse 中编译 Cygwin 项目

php - 从android在php服务器上上传多个图像

java - 执行所有 Eclipse 建议标记

android - 自定义文本跨度不起作用