Android TextView Text not getting wrapped

前端 未结 24 1438
时光说笑
时光说笑 2020-11-27 02:51

Can anyone tell me what\'s going wrong with the text? Text longer than one line doesn\'t wrap to the next line but goes beyond the screen.

Following is the code:

相关标签:
24条回答
  • 2020-11-27 03:14

    I just removed android:lines="1" and added android:maxLines="2", this got the text to wrap automatically. The problem was the android:lines attribute. That causes the text wrapping to not happen.

    I didnt have to use maxEms or singleLine="false" (deprecated API) to fix this.

    0 讨论(0)
  • 2020-11-27 03:17

    I could not get any of these solutions working when using a TableLayout>TableRow>TextView. I then found TableLayout.shrinkColumns="*". The only other solution that worked was forcing the TextView to layout_width:250px etc but i don't like forcing widths like that.

    Try something like this if working with tables.

                <TableLayout 
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:shrinkColumns="*">
    

    Note you need shrinkColumns="*"

    This is obviously within a <LinearLayout>. So something like <LinearLayout> <TableLayout> <TableRow> <TextView>

    references:

    TableLayout

    http://code.google.com/p/android/issues/detail?id=4000

    Hope that helps someone.

    0 讨论(0)
  • 2020-11-27 03:18

    I used android:ems="23" to solve my problem. Just replace 23 with the best value in your case.

    <TextView
            android:id="@+id/msg"
            android:ems="23"
            android:text="ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab "
            android:textColor="@color/white"
            android:textStyle="bold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    
    0 讨论(0)
  • 2020-11-27 03:19

    You must use 2 parameters :

    • android:ellipsize="none" : the text is not cut on textview width

    • android:scrollHorizontally="false" the text wraps on as many lines as necessary

    0 讨论(0)
  • 2020-11-27 03:21

    I fixed it myself, the key is android:width="0dip"

    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="4dip"
        android:layout_weight="1">
    
        <LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="4dip">
    
            <TextView
                android:id="@+id/reviewItemEntityName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/maroon"
                android:singleLine="true"
                android:ellipsize="end"
                android:textSize="14sp"
                android:textStyle="bold"
                android:layout_weight="1" />
    
            <ImageView
                android:id="@+id/reviewItemStarRating"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_alignParentBottom="true" />
            </LinearLayout>
    
            <TextView
                android:id="@+id/reviewItemDescription"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textSize="12sp"
                android:width="0dip" />
    
        </LinearLayout>
    
        <ImageView
            android:id="@+id/widget01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/arrow_nxt"
            android:layout_gravity="center_vertical"
            android:paddingRight="5dip" />
    
    </LinearLayout> 
    

    0 讨论(0)
  • 2020-11-27 03:21

    In my case, with a TableRow > ScrollView > TextView nesting, I solved the problem by setting android:layout_width to fill_parent on TableRow, and to wrap_content on ScrollView and TextView.

    0 讨论(0)
提交回复
热议问题