TextView not always expanding to a second line in TableLayout

妖精的绣舞 提交于 2019-12-11 04:07:43

问题


I have an app that is pulling information from a json file:

{
 "EVENTS": [
        {
            "what": " TABLE-QUIZ",
            "who": "anyone",
            "when": "31st January",
            "where": "ABC HELLO"
        }
    ]    
}

When I run the app, sometimes the text in the column When is cut off on smaller screens when it goes onto a second line. If the text in the What column is too big it will go onto a second line and not get cut off. Why is the When column not going onto the second line while the What column is?

Here is the layout xml that I am using:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:shrinkColumns="*"
android:stretchColumns="*" >

<TableRow
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/what"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/textlines"
        android:gravity="center"
        android:textColor="#fff"
        android:textSize="12sp" >

    </TextView>

    <TextView
        android:id="@+id/when"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="0.65"
        android:background="@drawable/textlines"
        android:gravity="center"
        android:textColor="#fff"
        android:textSize="12sp" >
    </TextView>

    <TextView
        android:id="@+id/where"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/textlines"
        android:gravity="center"
        android:textColor="#fff"
        android:textSize="12sp" >
    </TextView>

    <TextView
        android:id="@+id/forWho"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/textlines"
        android:clickable="true"
        android:gravity="center"
        android:onClick="onClick"
        android:textColor="#fff"
        android:textSize="12sp" >
    </TextView>
</TableRow>

</TableLayout>

EDIT: If I add:

android:minHeight="30dp"

to the TextView then I can get it to display the two lines, but I am hoping for a solution that will work automatically.


回答1:


The problem is that you set TableLayout's width (parent's width) to wrap_content. Setting it to match_parent like this

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    ...

makes When column go to the next line without being cut off.




回答2:


You need to view the full data means add this on Textview

android:singleLine="false" 

Hope this one will helps you.




回答3:


Change android:layout_weight to 1 and android:layout_width in all the TextViews to 0dp, so that layout_weight="1" property can work.

Hope this helps!




回答4:


I hope this helps

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:shrinkColumns="*"
    android:stretchColumns="*" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal" >

        <TextView
            android:id="@+id/textView41"
            android:text="when"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView42"
            android:gravity="center_horizontal"
            android:text="28°C" />

        <TextView
            android:id="@+id/textView43"
            android:gravity="center_horizontal"
            android:text="where" />

        <TextView
            android:id="@+id/textView44"
            android:gravity="center_horizontal"
            android:text="what" />

        <TextView
            android:id="@+id/textView44"
            android:gravity="center_horizontal"
            android:text="for who" />

    </TableRow>
</TableLayout>


来源:https://stackoverflow.com/questions/23163121/textview-not-always-expanding-to-a-second-line-in-tablelayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!