Android 中的多行 TextView?

发布于 2024-11-19 22:02:26 字数 523 浏览 3 评论 0原文

我确实像下面的 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...

所以我在这里做的错误是什么?

I did like below in 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>

It is not working for multiline, and I am using TableLayout...

so what is mistake I am doing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(21

晌融 2024-11-26 22:02:27

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

isMultiLine = 如果为 true,则 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);
    }

Below code can work for Single line and Multi-line textview

isMultiLine = If true then Textview showing with Multi-line otherwise single line

    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);
    }
梅窗月明清似水 2024-11-26 22:02:27

为什么不声明一个字符串而不是在布局文件中写入一长行。为此,您必须在布局文件 'android:text="@string/text"' 中声明一行代码,然后转到 \\app\src\main\res\values\strings.xml 并添加一行代码' 这里是您的多行文本' 还可以使用 '\n' 从任何点断开该行,否则该行将自动调整。

Why don't you declare a string instead of writing a long lines into the layout file. For this you have to declare a line of code into layout file 'android:text="@string/text"' and go to the \\app\src\main\res\values\strings.xml and add a line of code ' Your multiline text here' Also use '\n' to break the line from any point else the line will automatically be adjusted.

貪欢 2024-11-26 22:02:27

无需添加任何内容,只需在要更改行的位置使用“\n”即可,例如

    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" />

no need to add anything just use "\n" at the points where you want to change the lines e.g

<TextView
android:id="@+id/kyc_verify_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" />
∞琼窗梦回ˉ 2024-11-26 22:02:27

对我来说,没有一个解决方案不起作用。
我知道这不是这个问题的完整答案,但有时它会有所帮助。

我将 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>

此解决方案适用于文本视图文本恒定的情况,例如标签。

for me non of the solutions did not work.
I know it is not a complete answer for this question, but sometimes it can help.

I put 4 textviews in a vertical linearlayout.

<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>

This solution is good for cases that the text view text is constant, like labels.

泅渡 2024-11-26 22:02:27

当 TextView 无法获得足够的空间来容纳单行并且 singLine 未设置为 true 时,它​​将是多行的。

如果它在一行中获得空间,那么它就不会是多行。

TextView will be multi line when it wont get enough space to fit in the single line and singLine not set to true.

If it gets the space in one line it wont be multi line.

于我来说 2024-11-26 22:02:26

如果您放入 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>

If the text you're putting in the TextView is short, it will not automatically expand to four lines. If you want the TextView to always have four lines regardless of the length of the text in it, set the android:lines attribute:

<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>

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>
深爱成瘾 2024-11-26 22:02:26

我只是想我会补充一点,如果您使用:

android:inputType="textMultiLine"

然后视图将停止可点击。我试图在我的滑动抽屉菜单中获取多行文本视图,这显然需要响应点击。

不过 android:singleLine="false" 工作得很好。

I just thought I'd add that if you use :

android:inputType="textMultiLine"

Then the view stops being clickable. I was trying to get multi line textviews in my slide-drawer menu which obviously needs to respond to clicks.

The android:singleLine="false" worked fine though.

旧伤慢歌 2024-11-26 22:02:26

我不喜欢这两个答案。只需设置 inputType,TextView 将适应其内容

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

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

I like neither of the answers. Simply set the inputType and the TextView will adapt to its content

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

Tested on a Nexus One (2.3) and Nexus 4 (4.4)

你在我安 2024-11-26 22:02:26

只需将 '\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"/> 

Simply put '\n' inside your text... no extra attribute required :)

          <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"/> 
夏了南城 2024-11-26 22:02:26

只需在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>

Just add textview in ScrollView

<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>
自由如风 2024-11-26 22:02:26

我学到的是在想要插入下一行的单词之间添加“\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" />

5pm 和 By 之间的 \n 让我可以更好地控制下一行的开始和结束位置。

What I learned was to add "\n" in between the words where you want it to brake into the next line. For example...

<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" />

The \n between 5pm and By allowed me more control of where I wanted my my next line to begin and end.

娇纵 2024-11-26 22:02:26

尝试通过使其不可点击和不可聚焦来使用 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 -->
/>  

希望这有帮助:)

Try to work with EditText by make it unclickable and unfocusable, also you can display a scrollbar and delete the EditText's underbar.
Here is an example:

<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 -->
/>  

Hope this helps :)

丶视觉 2024-11-26 22:02:26

我不喜欢强制文本视图中的行数的解决方案。我宁愿建议您通过解决方案 此处提出。正如我所见,OP 也在努力使文本视图在表格中看起来正确,而 shr​​inkColumns 是传递以实现所需目的的正确指令。

I do not like the solution that forces the number of lines in the text view. I rather suggest you solve it via the solution proposed here. As I see the OP is also struggling with making text view look like proper in table and shrinkColumns is the correct directive to pass in to achieve what is wanted.

海拔太高太耀眼 2024-11-26 22:02:26

首先将 "\n" 替换为其 Html 等效的 "<br>"
然后对字符串调用 Html.fromHtml() 。请按照以下步骤操作:

String text= model.getMessageBody().toString().replace("\n", "<br>")
textView.setText(Html.fromHtml(Html.fromHtml(text).toString()))

这非常有效。

First replace "\n" with its Html equavalent "<br>"
then call Html.fromHtml() on the string. Follow below steps:

String text= model.getMessageBody().toString().replace("\n", "<br>")
textView.setText(Html.fromHtml(Html.fromHtml(text).toString()))

This works perfectly.

不美如何 2024-11-26 22:02:26

如果你想至少有 4 行,请添加 android:minLines="4"

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

如果你想始终有 4 行,请添加 android:lines="4"

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

如果您想要最多 4 行,请添加 android:maxLines="4"

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

if you want to have at least 4 lines add android:minLines="4"

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

if you want to have always 4 lines add android:lines="4"

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

and if you want to have a maximum of 4 lines add android:maxLines="4"

<TextView android:id="@+id/address1"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:gravity="left"
    android:maxLines="4"
    android:text="Johar Mor, Gulistan-e-Johar, Karachi" />
装纯掩盖桑 2024-11-26 22:02:26

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

android:inputType="textMultiLine"

The best answer I found for multiline TextView is:

android:inputType="textMultiLine"
画尸师 2024-11-26 22:02:26

最简单的方法

<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
希望它能帮助你

Simplest Way

<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>

Use \n where you want to insert a new line
Hopefully it will help you

停顿的约定 2024-11-26 22:02:26

我用过:

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

它对我有用。 1 是列数。

I used:

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

it worked for me. 1 is the number of column.

堇色安年 2024-11-26 22:02:26

关键是 Textview 与 singleline=false (是的,已弃用,但必须工作)与 linesmaxlines最小行数

<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"/>

The key is a Textview with singleline=false (which yes is deprecated, but is a must have to work) combined with lines, maxlinesor minlines

<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"/>
玻璃人 2024-11-26 22:02:26

我希望这些答案有点旧,只需更改资源布局文件中的输入类型即可解决您的问题。例如:

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

I hope these answers are little bit old, just change the input type in resource layout file will solve your problem. For example:

<EditText
        android:id="@+id/notesInput"
        android:hint="Notes"
        android:inputType="textMultiLine"
/>
怎言笑 2024-11-26 22:02:26
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>
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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文