代码之家  ›  专栏  ›  技术社区  ›  Andreas

TableLayout压碎按钮

  •  -1
  • Andreas  · 技术社区  · 6 年前

    我遇到了一个奇怪的问题 TableLayout 在a旁边 Button . 这是XML:

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto" 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">   
        <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/tableLayout1"
            android:stretchColumns="*"              
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" >    
            <TableRow
                    android:id="@+id/tableRow1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >    
                    <TextView
                    android:id="@+id/textView1"
                    android:text="Column 1" />
                    <Button
                        android:id="@+id/button1"
                        android:text="Column 2" />
            </TableRow>
        </TableLayout>
        <Button  
            android:id="@+id/button2"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content" 
            android:layout_weight="1"           
            android:text="Button 2" />                          
    </LinearLayout> 
    

    这就是我得到的:

    Screenshot

    这让我很困惑。如你所见 按钮 以及 表格布局 有一个 layout_weight 共1页。所以我希望它们在 LinearLayout 但出于某种原因 表格布局 占用了更多的空间并一直向右按按钮。

    我怎样才能在 线性布局 分布在 表格布局 以及 按钮 根据中设置的值 布局重量 拜托?

    注意,我不想使用 RelativeLayout ConstraintLayout . 我特别想找一个 线性布局 解决方案。

    2 回复  |  直到 6 年前
        1
  •  0
  •   Sachin Rajput KgaboL    6 年前

    把这个加到你父母身上。使宽度 0dp 两个孩子的

    android:weightSum=“2”系统

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:weightSum="2">
    
        <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/tableLayout1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:stretchColumns="*">
    
            <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
    
                <TextView
                    android:id="@+id/textView1"
                    android:text="Column 1" />
    
                <Button
                    android:id="@+id/button1"
                    android:text="Column 2" />
            </TableRow>
        </TableLayout>
    
        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button 2" />
    </LinearLayout> 
    

    enter image description here

        2
  •  0
  •   Shubham Vala    6 年前

    设置 android:layout_width="0dp" 两者都有 Button TableLayout