宽度为“wrap_content”的多行TextView

时间:2013-03-06 21:33:21

标签: android textview

我想知道如何TextViewwithout hardcoding the width in the XML几行显示其内容。

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="false"
            android:text="Long multiline text"/>

        <TextView
            android:textColor="@color/text_color"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            />

    </LinearLayout>

欢迎任何想法。

编辑:我的问题是,当文本超出设置的宽度时(因为它到达屏幕的末尾),文本的一部分才会显示。我希望文本分为两行

4 个答案:

答案 0 :(得分:17)

虽然我无法重现无包装问题,但您可以在第一个weight上使用TextView来解决定位问题。使用以下XML在Eclipse的图形布局视图中提供了预期的输出:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:orientation="horizontal">

    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:singleLine="false"
        android:text="Long multiline text"/>

    <TextView
        android:textColor="@color/text_color"
        android:layout_width="130dp"
        android:layout_height="wrap_content"
        />

</LinearLayout>

答案 1 :(得分:1)

同时添加

android:minLines="2"
android:scrollHorizontally="false"

答案 2 :(得分:0)

你可以尝试

android:inputType="textMultiLine"
TextView XML中的

。这对我有用。

答案 3 :(得分:0)

我认为我有非常相似的问题。我有一个带有文本的TextView,但不确定要花多少行。它由具有LinearLayout的{​​{1}}封装,以确保我的文字将水平填充所有空间。但是,问题是我的文本不适合第一行,当它变成新行时,其下一个视图组件也没有向下移动,从而没有足够的空间使第二行完全可见。

我可以通过将包含我的android:layout_width="match_parent"的{​​{1}}更改为 LinearLayout 来实现解决方案。这样,文本下面的元素(实际上是Layout本身下面的元素)将自动移动,以为多行文本留出足够的空间。

相关问题