how to wrap text in textview in android

2019-01-23 00:17发布

Does any one know how to wrap text in TextView in Android platform. i.e if the text in textview exceed the screen length it should be displayed in the second line.

I have searched and tried the following:

android:scrollHorizontally="false",
android:inputType="textMultiLine",
android:singleLine="false"

But none work..

Can anyone suggest how can i do it.. i am relatively new to android platform..

16条回答
手持菜刀,她持情操
2楼-- · 2019-01-23 00:49

For the case where the TextView is inside a TableLayout, the solution is to set android:shrinkColumns="1" on the TableLayout. (Replace 1 with the column number the TextView you want to wrap is in. (0-indexed))

AFAICT, no other attributes are needed on the TextView.

For other cases, see the other answers here.

FWIW, I had initially gotten it to sort of work with

 <TextView
   android:id="@+id/inventory_text"
   android:layout_width="fill_parent"
   android:layout_weight="1"
   android:width="0dp"

but that resulted in some extra empty space at the bottom of the Dialog it was all in.

查看更多
来,给爷笑一个
3楼-- · 2019-01-23 00:49

Try @Guykun's approach and make sure that parents width is not set to wrap content. This is the thing that I was missing.

查看更多
姐就是有狂的资本
4楼-- · 2019-01-23 00:51

For me this issue only occurred on Android < 4.0

The combination of parameters I used were:

android:layout_weight="1"
android:ellipsize="none"
android:maxLines="100"
android:scrollHorizontally="false"

The maxLines count seemed to be the random final piece that made my TextView wrap.

查看更多
小情绪 Triste *
5楼-- · 2019-01-23 00:53

OK guys the truth is somewhere in the middle cause you have to see the issue from the parent's view and child's. The solution below works ONLY when spinner mode = dialog regardless of Android version (no problem there.. tested it in VD and DesireS with Android =>2.2) :

  1. .Set you spinner's(the parent) mode like :

    android:spinnerMode="dialog"  
    
  2. Set the textview's(child custom view) properties to :

    android:layout_weight="1"  
    android:ellipsize="none"  
    android:maxLines="100"  
    android:scrollHorizontally="false"
    

I hope this works for you also.

查看更多
女痞
6楼-- · 2019-01-23 00:57

You need to add your TextView in a ScrollView with something like this :

<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>
查看更多
看我几分像从前
7楼-- · 2019-01-23 00:58

Strange enough - I created my TextView in Code and it wrapped - despite me not setting anything except standard stuff - but see for yourself:

LinearLayout.LayoutParams childParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT);
childParams.setMargins(5, 5, 5, 5);

Label label = new Label(this);
label.setText("This is a testing label This is a testing label This is a testing label This is a testing labelThis is a testing label This is a testing label");
label.setLayoutParams(childParams);

As you can see from the params definition I am using a LinearLayout. The class Label simply extends TextView - not doing anything there except setting the font size and the font color.

When running it in the emulator (API Level 9) it automatically wraps the text across 3 lines.

查看更多
登录 后发表回答