为什么布局不符合规定的重量?

Isp*_*diu 4 layout android android-layout android-linearlayout

假设我有一个LinearLayout(宽度和高度match_parent),weightSum ="5",然后是水平方式

我将5个TableLayout(width&height wrap_content)与layout_weight ="1"放在一起,因此每个TableLayout都是其父级的1/5.

但是,在每个TableLayout之后我放了一个TextView(宽度和高度wrap_content)并且每件事都出错了(从我的角度来看):

如果文本是长那么它迫使其父布局(在我们的例子TableLayout),以消耗等等TableLayout从它的LinearLayout父的1/5比例不再受到尊重,即使我指定的TextView,S ellipsize ="结束".

做了一些研究之后我发现设置layout_width比其他所有属性(比如weightSum).

在我的情况下,我设置了TableLayout的width = wrap_content,但也设置了layout_weight ="1",但由于它是wrap_content,它往往会在其内容之后消耗,而不是考虑权重.

任何人都可以给我一些解决方案,我的表格布局如何尊重重量?(因为我不希望我的布局不遵守子文本视图长度后的比例和消耗).

谢谢你的好意.这是我的代码:

<!-- 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>
Run Code Online (Sandbox Code Playgroud)

Ale*_*lak 5

外部TableLayouts使用android:layout_width ="0dp"而不是android:layout_width ="wrap_content".

同时指定android:layout_weight ="1".

删除android:weightSum ="5"以自动调整大小.

<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>
Run Code Online (Sandbox Code Playgroud)

为什么TableLayout里面有空?如果你想看全文然后设置android:ellipsize ="none"