Android 中的多行文本视图 Multiline TextView? ?

我喜欢下面的 xml

<TableRow>
<TextView android:id="@+id/address1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:maxLines="4"
android:singleLine="false"
android:text="Johar Mor, Gulistan-e-Johar, Karachi" >
</TextView>
</TableRow>

这是不工作的 multiline,我正在使用 TableLayout..。

我到底犯了什么错?

385594 次浏览

当 TextView 没有足够的空间容纳单行和 singLine 没有设置为 true 时,它将是多行。

如果它得到一行中的空格,它就不会是多行。

如果你在 TextView中输入的文本很短,它不会自动扩展到四行。如果您希望 TextView一直都是有四行,而不管其中的文本长度如何,请设置 android:lines属性:

<TextView
android:id="@+id/address1"
android:gravity="left"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:maxLines="4"
android:lines="4"
android:text="Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."></TextView>

您可以使用 TableRow 完成此操作,请参见下面的代码

<TableRow >
<TextView
android:id="@+id/tv_description_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:padding="8dp"
android:text="@string/rating_review"
android:textColor="@color/black"
android:textStyle="bold" />


<TextView
android:id="@+id/tv_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:maxLines="4"`enter code here`
android:padding="8dp"
android:text="The food test was very good."
android:textColor="@color/black"
android:textColorHint="@color/hint_text_color" />
</TableRow>

我不喜欢强制文本视图中的行数的解决方案。我宁愿建议你通过解决方案 在这里求婚解决它。正如我所看到的 OP 也挣扎在使文本视图看起来像适当的表和 shrinkColumns是正确的指令传递,以实现所需要的。

这两个答案我都不喜欢。只需设置 inputType,TextView 就会适应它的内容

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"/>

在 Nexus One (2.3)和 Nexus 4(4.4)上进行测试

只需在 ScrollView 中添加 textview

<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="20dp"
android:fillViewport="true">


<TextView
android:id="@+id/txtquestion"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="@drawable/abs__dialog_full_holo_light"
android:lines="20"
android:scrollHorizontally="false"
android:scrollbars="vertical"
android:textSize="15sp" />


</ScrollView>

我用:

TableLayout tablelayout = (TableLayout) view.findViewById(R.id.table);
tablelayout.setColumnShrinkable(1,true);

这对我很有用。1是列的数字。

我只是觉得如果你用:

android:inputType="textMultiLine"

然后视图停止单击。我试图在我的幻灯片抽屉菜单中获得多行文本视图,这显然需要响应点击。

但是 android:singleLine="false"运行良好。

只需将’n’放在文本中... 不需要额外的属性:)

          <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Pigeon\nControl\nServices"
android:id="@+id/textView5"
android:layout_gravity="center"
android:paddingLeft="12dp"/>

为什么不声明一个字符串,而不是在布局文件中写一长行。为此,你必须在布局文件‘ android: text = “@string/text”中声明一行代码,然后进入应用程序 src main res value strings.xml 并添加一行代码‘ Your multiline text here’同样使用’n’从任何其他地方中断该行,该行将自动调整。

我学到的是在单词之间加上“ n”,这样你就可以让它停在下一行。比如说..。

<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="M-F 9am-5pm \n By Appointment Only" />

下午5点和 By 之间的 n 使我能够更好地控制下一行的开始和结束。

尝试使用 EditText,使其不可点击和不可聚焦,还可以显示滚动条并删除 EditText 的下栏。
这里有一个例子:

<EditText
android:id="@+id/my_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_parent"
android:inputType="textMultiLine"                   <!-- multiline -->
android:clickable="false"                         <!-- unclickable -->
android:focusable="false"                         <!-- unfocusable -->
android:scrollbars="vertical"     <!-- enable scrolling vertically -->
android:background="@android:color/transparent"   <!-- hide the underbar of EditText -->
/>

希望这对你有帮助:)

对于多行 TextView,我找到的最佳答案是:

android:inputType="textMultiLine"

首先用等效于 Html"&lt;br&gt;"代替 "\n" 然后调用字符串上的 Html.fromHtml()。按照以下步骤:

String text= model.getMessageBody().toString().replace("\n", "&lt;br&gt;")
textView.setText(Html.fromHtml(Html.fromHtml(text).toString()))

这样很好。

对我来说,没有一个解决办法是行不通的。 我知道这不是这个问题的完整答案,但有时它可以帮助。

我将4个文本视图放在垂直线性布局中。

<Linearlayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation:"vertical"


...>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text:"line1"
.../>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text:"line2"
.../>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text:"line3"
.../>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text:"line4"
.../>
</LinearLayout>

这个解决方案对于文本视图文本是常量(如标签)的情况非常有用。

You can do this with TableRow, see below code


<TableRow >
<TextView
android:id="@+id/tv_description_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:padding="8dp"
android:text="@string/rating_review"
android:textColor="@color/black"
android:textStyle="bold" />


<TextView
android:id="@+id/tv_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:maxLines="4"`enter code here`
android:padding="8dp"
android:text="The food test was very good."
android:textColor="@color/black"
android:textColorHint="@color/hint_text_color" />
</TableRow>

你可以使用 android: inputType = “ textMultiLine”

  <TextView android:id="@+id/address1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:maxLines="4"
android:inputType="textMultiLine"
android:text="Johar Mor, Gulistan-e-Johar, Karachi" />

最简单的方法

<TableRow>
<TextView android:id="@+id/address1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:maxLines="4"
android:singleLine="false"
android:text="Johar Mor,\n Gulistan-e-Johar,\n Karachi" >
</TextView>
</TableRow>

在要插入新行的位置使用 \n 希望能帮到你

关键是一个带有 singleline=false的 Textview (是的,不推荐使用,但必须使用)与 linesmaxlinesminlines相结合

<TextView
android:id="@+id/txtBowlers"
style="@style/Default_TextBox.Small"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:singleLine="false"
android:maxLines="6"
android:text="Bob\nSally\nJohn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/txtTeamName"
app:layout_constraintTop_toBottomOf="@+id/txtTeamName"/>

我希望这些答案有点老,只要改变资源布局文件中的输入类型就可以解决你的问题。例如:

<EditText
android:id="@+id/notesInput"
android:hint="Notes"
android:inputType="textMultiLine"
/>

下面的代码可以用于单行和多行文本视图

IsMultiLine = 如果为真,则 Textview 显示为多行,否则为单行

    if (isMultiLine) {
textView.setElegantTextHeight(true);
textView.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
textView.setSingleLine(false);
        

} else {
textView.setSingleLine(true);
textView.setEllipsize(TextUtils.TruncateAt.END);
}

不需要添加任何东西,只需在你想要改变行的地方使用“ n”即可

< TextView Android: id = “@+ id/kyc _ valid_ textbox”

    android:layout_width="370dp"
android:layout_height="99dp"
android:layout_marginTop="6dp"
android:layout_marginBottom="13dp"


android:text="For KYC verification, \n please submit: \n 1 RC photo \n 2 Truck Owner Details \n Note:Both are mandatory for KYC verification"
app:layout_constraintBottom_toTopOf="@+id/divider2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/divider" />