【问题标题】:Android Eclipse Linear Layout with multiple text views not wrapping具有多个文本视图的 Android Eclipse 线性布局不换行
【发布时间】:2012-04-16 21:48:26
【问题描述】:

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

我该怎么做才能使具有多个文本视图的线性布局在超出父级宽度的情况下包裹文本视图?

这是我的布局中的一个 sn-p:

<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 具有相同的大小宽度?
  • 不,它们不需要大小相同。它们将根据某人稍后输入的内容而有所不同。我只是想要它,这样如果文本视图中的文本变得太长以至于不能放在一个喜欢的地方——它将换行到下一行。现在如果文本太长,它会将最后一个文本视图向右打碎,使其变成多行,但每行只有 1-2 个字符全部向右打碎

标签: android eclipse textview android-linearlayout


【解决方案1】:

使用包含多个 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        |            |                                            |   |

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

【讨论】:

  • 这是有道理的。感谢您简洁明了的回答
【解决方案2】:

您无法使用 LinearLayout 实现该结果。我认为在这种情况下最好的解决方案是使用具有多种样式的单个TextView(如果您需要文本的动态值)。看看这里的见解: Is it possible to have multiple styles inside a TextView?

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    • 2011-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    相关资源
    最近更新 更多