有没有人知道如何在Android平台中的TextView中换行文本。即,如果textview中的文本超过屏幕长度,则应在第二行显示。
我已经搜索并尝试了以下内容:
android:scrollHorizontally="false",
android:inputType="textMultiLine",
android:singleLine="false"
但是没有一个起作用...
有人能建议我该如何做吗。
<TextView
android:id="@+id/some_textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="@id/textview_above"
app:layout_constraintRight_toLeftOf="@id/button_to_right"/>
android:maxLines="2"
以防止垂直扩展(2只是一个例子)android:ellipsize="end"
0dp宽度允许左/右约束确定您的小部件有多宽。
设置左/右约束设置实际宽度,文本将根据其中自动换行。
对我来说,这个问题只在Android < 4.0上出现。
我使用的参数组合是:
android:layout_weight="1"
android:ellipsize="none"
android:maxLines="100"
android:scrollHorizontally="false"
maxLines 的数量似乎是让我的 TextView 换行的随机关键因素。
如果 TextView
位于 TableLayout
中,解决方案是在 TableLayout
上设置 android:shrinkColumns="1"
。(将 1
替换为要包装的 TextView 所在的列号(从 0 开始)。)
据我所知,TextView
上不需要任何其他属性。
对于其他情况,请参见此处的其他答案。
顺便说一句,我最初用以下方式使其部分工作:
<TextView
android:id="@+id/inventory_text"
android:layout_width="fill_parent"
android:layout_weight="1"
android:width="0dp"
但这导致对话框底部出现了一些额外的空白。
在
AppCompatTextView
中使用app:breakStrategy="simple"
,它会控制段落布局。
它有三个常量值:
在您的TextView xml中进行设计
<android.support.v7.widget.AppCompatTextView
android:id="@+id/textquestion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:scrollHorizontally="false"
android:text="Your Question Display Hear....Your Question Display Hear....Your Question Display Hear....Your Question Display Hear...."
android:textColor="@android:color/black"
android:textSize="20sp"
android:textStyle="bold"
app:breakStrategy="simple" />
如果您当前的最低API级别为23或更高,则在编码中:yourtextview.setBreakStrategy(Layout.BREAK_STRATEGY_SIMPLE);
如需更多参考,请参阅BreakStrategy
您必须使用两个参数:
android:ellipsize="none"
:文本不会在文本视图宽度上被截断
android:scrollHorizontally="false"
:文本根据需要换行到尽可能多的行
android:layout_weight="1"
。layout_weight="1"
修复了我的问题。然而,我的包含TextView的TableRow,layout_height
设为特定值。也许对某些人有用。 - kit好的,各位,事实上真相在两者之间,因为你需要从家长和孩子的角度来看待这个问题。以下解决方案仅适用于旋转模式=dialog
,无论Android版本(在VD和DesireS上测试过,Android=>2.2没有问题):
.Set you spinner's(the parent) mode like :
android:spinnerMode="dialog"
Set the textview's(child custom view) properties to :
android:layout_weight="1"
android:ellipsize="none"
android:maxLines="100"
android:scrollHorizontally="false"
android:maxEms
的值,再搭配android:layout_weight="1"
属性,当TextView达到ems的给定长度时,它将自动换行。inputType
属性下有一个名为textMultiLine
的属性。选择此选项可以解决类似问题。希望这能帮到您。我刚刚在RecyclerView内部的布局中操作了一个TextView。我的文本被割掉了,例如对于Read this message
,我只看到Read this
。我尝试在TextView上设置android:maxLines="2"
,但是没有任何变化。然而,android:lines="2"
会在第一行显示Read this
,第二行显示message
。
android:inputType="textPersonName"
**。显然,这是由Android Studio设计工具添加的。删除该属性解决了问题!可能还有其他输入类型也会阻止换行。 - LarsH