Home > OS >  layout_width attribute of TableLayout children
layout_width attribute of TableLayout children

Time:10-29

While going through the Android developer docs for TableLayout , I saw a line mentioning

The children of a TableLayout cannot specify the layout_width attribute."

But in code if I use the layout_width attribute with TextView, the width gets increased accordingly.

Am I missing something?

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="vertical"
android:stretchColumns="*"
>

<TableRow android:padding="5dip">

    <TextView
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:gravity="center"
        android:text="loginForm"
        android:textColor="#0ff"
        android:textSize="25sp"
        android:textStyle="bold" />
</TableRow>

<TableRow>

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="200dp"
        android:layout_marginLeft="10dp"
        android:text="User Name"
        android:textColor="#fff"
        android:textSize="16sp" />

    <EditText
        android:id="@ id/userName"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:background="#fff"
        android:padding="5dp"
        android:textColor="#000" />

</TableRow>

<TableRow>

    <TextView
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="20dp"
        android:text="password"
        android:textColor="#fff"
        android:textSize="16sp" />

    <EditText
        android:id="@ id/password"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="20dp"
        android:background="#fff"
        android:padding="5dp"
        android:textColor="#000" />

</TableRow>

</TableLayout>

CodePudding user response:

I believe it is talking about the first level child component inside the TableLayout component.

If you add a TextView at the same level as your TableRow, the width will cannot be adjusted.

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="vertical"
android:stretchColumns="*">

<!--This TextView will fail to adjust its width-->
<TextView
      android:text="123"/>

<TableRow android:padding="5dip">
    <TextView
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:gravity="center"
        android:text="loginForm"
        android:textColor="#0ff"
        android:textSize="25sp"
        android:textStyle="bold" />
</TableRow>

...

</TableLayout>`

CodePudding user response:

The wording of the docs is really not clear enough, I think it should be more like

"The direct children of a TableLayout cannot specify the layout_width attribute."

i.e the Textview is a grandchild of the TableLayout and a child of the TableRow

But on a TableRow

The children of a TableRow do not need to specify the layout_width and layout_height attributes in the XML file.

So remember that a TableLayout fits the TableRow to the width of the Table and will resize row items (children of the row) if needed (including details growing/shrinking based on these attributes and change them based on the size of the column in other rows)

So one of the first thing TableLayout does to work out the size of the columns by finding out the size that the children would like to be with a limit on the max size determined by the parent view.

So in your instance you are making the Texview with the width attribute the widest cell in the column and because after the calculated column sizes are less than the width of the table and it can honour that size.

If you had widths on other Textview's in that column that are bigger you would find that it would not honour each width.

So basically it will try and honour the Textview's width attribute BUT there are many instances when it won't be able to.

  • Related