Why the layout does not match the specified weight?

Suppose I have a LinearLayout (width and height match_parent) with weightSum = "5" and then horizontally

I put 5 TableLayout ( wrap_content width and height) using layout_weight = "1" , so each TableLayout is 1/5 of its parent element.

But after that, in each TableLayout I put a TextView (width and height of wrap_content), and everything goes wrong (from my point of view):

if the text is long, then it forces its parent layout (in our case, TableLayout) to spend , and therefore 1/5 of the TableLayout from LinearLayout parent is no longer respected, although I specified TextView, s ellipsize = "end".

Well, after doing some reserch, I found that setting the layout_width is stronger than any other attribute (like weightSum) .

In my case, I set TableLayout width = wrap_content , but also layout_weight = "1" , but since it is wrap_content, it is usually consumed after its contents and does not respect weight.

Can someone give me some solutions how my table layout will respect weight? (because I don’t want my layout to be out of proportion and spent after the length of the children's text view).

Thank you for your kind image. Here is my code:

<!-- my code --> < LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:weightSum="5" > <!-- table 1 --> <TableLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:ellipsize="end" android:maxLines="1" android:text="test1test2test3test4test5" android:textSize="20dp"/> <TableLayout android:layout_width="match_parent" android:layout_height="match_parent"> </TableLayout> </TableLayout> <!-- table 2 --> <TableLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="test" android:textSize="20dp" /> <TableLayout android:layout_width="match_parent" android:layout_height="match_parent"> </TableLayout> </TableLayout> <!-- table 3 --> <TableLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="test" android:textSize="20dp" /> <TableLayout android:layout_width="match_parent" android:layout_height="match_parent"> </TableLayout> </TableLayout> <!-- table 4 --> <TableLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="test" android:textSize="20dp" /> <TableLayout android:layout_width="match_parent" android:layout_height="match_parent"> </TableLayout> </TableLayout> <!-- table 5 --> <TableLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="test" android:textSize="20dp" /> <TableLayout android:layout_width="match_parent" android:layout_height="match_parent"> </TableLayout> </TableLayout> </LinearLayout> 
+4
source share
4 answers

Use android: layout_width = "0dp" instead of android: layout_width = "wrap_content" for external TableLayouts.

Also specify android: layout_weight = "1".

Remove android: weightSum = "5" to automatically resize.

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <!-- table 1 --> <TableLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="end" android:gravity="center_horizontal" android:text="test1 test2 test3 test4 test5" android:textSize="20dp" /> <TableLayout android:layout_width="match_parent" android:layout_height="match_parent" > </TableLayout> </TableLayout> <!-- table 2 --> <TableLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="test" android:textSize="20dp" /> <TableLayout android:layout_width="match_parent" android:layout_height="match_parent" > </TableLayout> </TableLayout> <!-- table 3 --> <TableLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="test" android:textSize="20dp" /> <TableLayout android:layout_width="match_parent" android:layout_height="match_parent" > </TableLayout> </TableLayout> <!-- table 4 --> <TableLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="test" android:textSize="20dp" /> <TableLayout android:layout_width="match_parent" android:layout_height="match_parent" > </TableLayout> </TableLayout> <!-- table 5 --> <TableLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="test" android:textSize="20dp" /> <TableLayout android:layout_width="match_parent" android:layout_height="match_parent" > </TableLayout> </TableLayout> </LinearLayout> 

Why do you have an empty inside TableLayout? If you want to see all the text, then set android: ellipsize = "none"

+5
source

android:layout_width= "0dp" for all five parent table layouts instead of " wrap_content"

+3
source

set the layout_height and layout_width table values ​​to "fill_parent" for all your layouts. and you will do it.

0
source

You can use Table Row in the layout of one table and use weight_sum to layout the table

  ` <TableLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:weightSum="5"/> <TableRow your text /> <TableRow your text /> <TableRow your text /> <TableRow your text /> <TableRow your text /> </TableLayout>` 
0
source

All Articles