繁体   English   中英

宽度为“wrap_content”的多行TextView

[英]Multiline TextView with width “wrap_content”

我想知道如何让TextView在几行上显示其内容而不用硬编码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>

任何想法都欢迎。

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

虽然我无法重现非包装问题,但您可以通过在第一个TextView上使用weight来修复定位问题。 使用以下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>

还添加

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

你可以试试

android:inputType="textMultiLine"

在TextView XML中。 这对我有用。

我想我的问题非常相似。 我有一个带有文本的TextView ,我不确定需要多少行。 它由具有android:layout_width="match_parent"LinearLayout封装,以确保我的文本将水平填充所有空间。 然而,问题是我的文本不适合1行,当它突然变成新行时,它下面的下一个视图组件没有向下移动以提供足够的空间以使第二行完全可见。

我可以通过将包含TextViewLinearLayout更改为RelativeLayout来实现该解决方案。 通过这种方式,文本下方的元素(实际上在布局本身下方)被自动移动,为多行文本提供足够的空间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM