【问题标题】:Why doesn't the layout stick with the weight specified?为什么布局不坚持指定的重量?
【发布时间】:2012-11-25 11:17:44
【问题描述】:

假设我有一个 LinearLayout(width & height match_parent) 与 weightSum = "5",然后以 水平方式

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

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

如果文本太长,则它强制其父布局(在我们的例子中为 TableLayout)expend,因此 TableLayout 的 1/5 比例来自其 LinearLayout即使我指定了 TextView,s ellipsize="end",也不再尊重父级。

在做了一些研究之后,我发现设置 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>

【问题讨论】:

    标签: android android-layout layout android-linearlayout


    【解决方案1】:

    对外部 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>
    

    为什么TableLayout里面有空? 如果您想查看整个文本,请设置 android:ellipsize="none"

    【讨论】:

    • 非常感谢您的回答!它的工作原理。我不知道 0 dp 然后设置权重,但这很好:)。关于里面的 TableLayout 它将为我未来的工作填充其他视图。
    【解决方案2】:

    android:layout_width= "0dp" 用于所有五个父表布局,而不是“wrap_content"

    【讨论】:

      【解决方案3】:

      将所有表格布局的 layout_height 和 layout_width 设置为“fill_parent”。 你就完成了。

      【讨论】:

        【解决方案4】:

        您可以在一个表格布局中使用 Table Row,并在表格布局中使用 weight_sum

               ` <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>`
        

        【讨论】:

          猜你喜欢
          • 2011-04-22
          • 2021-03-10
          • 2012-07-22
          • 2023-03-05
          • 1970-01-01
          • 1970-01-01
          • 2013-06-06
          • 2016-09-18
          • 2020-08-17
          相关资源
          最近更新 更多