Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't the layout stick with the weight specified?

Suppose that i have a LinearLayout(width & height match_parent) with weightSum = "5", then in horizontal way

I put 5 TableLayout(width & height wrap_content) with layout_weight = "1", so every TableLayout is 1/5 of its parent.

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

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

Well after doing some reserch i found that setting layout_width is stronger than every other attribute(like weightSum).

In my case I set TableLayout's width=wrap_content but also layout_weight="1" but because it is wrap_content, it tends to expend after its content and not to respect the weight.

Can anyone give me some solutions , how can my table layout respect the weight?(because i don't want my layout not to respect the proportions and expend after the length of the child text view).

Thank you for your kind. 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>
like image 645
Ispas Claudiu Avatar asked Feb 18 '23 06:02

Ispas Claudiu


1 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" for automatic resizing.

<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 empty inside TableLayout? If you want to see whole text then set android:ellipsize="none"

like image 97
Aleksander Gralak Avatar answered Mar 02 '23 15:03

Aleksander Gralak