[英]Multiline TextView with width “wrap_content”
I am wondering how to have a TextView
display its content on several lines without hardcoding the width in the XML . 我想知道如何让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>
Any thought welcome. 任何想法都欢迎。
EDIT : my problem is that when the text exceeds the width set (because it reaches the end of the screen) a portion of the text is just not displayed. 编辑 :我的问题是,当文本超过设置的宽度(因为它到达屏幕的末尾)时,文本的一部分不会显示。 I would expect the text to be split on two lines 我希望文本分为两行
Though I cannot reproduce the not wrapping problem, you can fix the positioning problem by using a weight
on the first TextView
. 虽然我无法重现非包装问题,但您可以通过在第一个TextView
上使用weight
来修复定位问题。 Using the following XML gives the expected output in the graphical layout view in Eclipse: 使用以下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>
Also add 还添加
android:minLines="2"
android:scrollHorizontally="false"
You could try 你可以试试
android:inputType="textMultiLine"
in your TextView XML. 在TextView XML中。 This worked for me. 这对我有用。
I think I had very similar problem. 我想我的问题非常相似。 I had a TextView
with a text, where I was not sure how much lines will it take. 我有一个带有文本的TextView
,我不确定需要多少行。 It was encapsulated by a LinearLayout
having android:layout_width="match_parent"
to ensure my text will fill out all the space horizontally. 它由具有android:layout_width="match_parent"
的LinearLayout
封装,以确保我的文本将水平填充所有空间。 However, the problem was that my text did not fit into 1 line and when it did break into a new line, the next view component below it did not move downwards to give enough space for the second line to be viewable fully. 然而,问题是我的文本不适合1行,当它突然变成新行时,它下面的下一个视图组件没有向下移动以提供足够的空间以使第二行完全可见。
I could achieve the solution by changing the LinearLayout
that was containing my TextView
into a RelativeLayout
. 我可以通过将包含TextView
的LinearLayout
更改为RelativeLayout
来实现该解决方案。 By this way, the element below the text (actually below the Layout itself) was moved automatically to give enough space for the multi-line text. 通过这种方式,文本下方的元素(实际上在布局本身下方)被自动移动,为多行文本提供足够的空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.