如何在android中的textview中包装文本

Sam*_*eer 76 android word-wrap textview

有没有人知道如何在Android平台的TextView中包装文本.即如果textview中的文本超出屏幕长度,则应在第二行显示.

我搜索并尝试了以下内容:

android:scrollHorizontally="false",
android:inputType="textMultiLine",
android:singleLine="false"
Run Code Online (Sandbox Code Playgroud)

但没有工作..

任何人都可以建议我怎么做..我是Android平台相对较新的..

Guy*_*kun 50

对我来说,此问题仅发生在Android <4.0上

我使用的参数组合是:

android:layout_weight="1"
android:ellipsize="none"
android:maxLines="100"
android:scrollHorizontally="false"
Run Code Online (Sandbox Code Playgroud)

maxLines计数似乎是使我的TextView包装的随机最终部分.

  • 我现在知道它是一个旧线程,但我想我会发布我的发现.我只使用'android:maxLines'然后它包裹了我的文本.欢呼@Guykun (8认同)

aij*_*aij 31

对于TextView在a内的情况TableLayout,解决方案是设置android:shrinkColumns="1"TableLayout.(替换1为要包装的TextView所在的列号.(0-indexed))

AFAICT,不需要其他属性TextView.

对于其他情况,请在此处查看其他答案.

FWIW,我最初得到了它的工作

 <TextView
   android:id="@+id/inventory_text"
   android:layout_width="fill_parent"
   android:layout_weight="1"
   android:width="0dp"
Run Code Online (Sandbox Code Playgroud)

但这导致Dialog底部有一些额外的空白空间.


Bak*_*ker 30

约束布局

<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"/>
Run Code Online (Sandbox Code Playgroud)
  • 确保您的布局宽度为零
  • 左/右约束已定义
  • wrap_content的布局高度允许向上/向下扩展.
  • 设置android:maxLines="2"为防止垂直扩展(2只是例如)
  • 椭圆是可能的.最大线条的好主意android:ellipsize="end"

0dp宽度允许左/右约束来确定窗口小部件的宽度.

设置左/右约束设置窗口小部件的实际宽度,文本将在其中包装.

约束布局文档

  • 是的!这就是它的工作原理!只要确保将宽度设置为 0dp (2认同)

Nir*_*sar 11

使用app:breakStrategy="simple"AppCompatTextView,它会在控制段落布局。

它有三个常数值

  • 均衡
  • 高质量
  • 简单的

在你的 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" />
Run Code Online (Sandbox Code Playgroud)

如果您当前的最低 api 级别为 23 或更高,则在编码中

yourtextview.setBreakStrategy(Layout.BREAK_STRATEGY_SIMPLE);
Run Code Online (Sandbox Code Playgroud)

有关更多参考,请参阅此BreakStrategy

Textview breakStrategy 图像


Kar*_*uez 10

您必须使用2个参数:

  • android:ellipsize="none" :文本未在文本视图宽度上剪切

  • android:scrollHorizontally="false" 文本根据需要包含尽可能多的行

  • 那不适合我.(FWIW,我在TableLayout中的TableRow中有一个TextView.) (3认同)

use*_*121 7

这应该可以解决你的问题:android:layout_weight="1".

  • 在 TableLayout 的情况下,`layout_weight="1"` 为我解决了这个问题。但是,我的包含 TextView 的 TableRow 将 `layout_height` 设置为特定值。也许对某人有用 (3认同)

sko*_*kos 5

好吧,伙计们,事实是在中间的某个地方,因为你必须从父母的观点和孩子的角度看问题.下面的解决方案仅适用于微调器模式= dialog无论Android版本(没有问题......在VD和DesireS中使用Android => 2.2测试它):

  1. .设置微调器(父)模式,如:

    android:spinnerMode="dialog"  
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将textview的(子自定义视图)属性设置为:

    android:layout_weight="1"  
    android:ellipsize="none"  
    android:maxLines="100"  
    android:scrollHorizontally="false"
    
    Run Code Online (Sandbox Code Playgroud)

我希望这对你也有用.


Fre*_*oid 5

通过设置android:maxEms为给定值,android:layout_weight="1"将导致 TextView 在达到给定的 em 长度后换行。


Lud*_*dry -1

您需要将 TextView 添加到 ScrollView 中,如下所示:

<ScrollView android:id="@+id/SCROLL_VIEW"  
android:layout_height="150px"   
android:layout_width="fill_parent">  

<TextView   
   android:id="@+id/TEXT_VIEW"   
   android:layout_height="wrap_content"   
   android:layout_width="wrap_content"   
   android:text="This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header" />  
 </ScrollView>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢您的回复,但我的目标是当文本超过行长时将其换行,而不是放置水平滚动条。 (4认同)