XML表格布局?两个EQUAL宽度的行填充相同宽度的按钮?

Oli*_*ens 36 xml android width android-tablelayout

这是我的XML for LAND格式的一部分:

<TableLayout
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="center"
    android:stretchColumns="*">
<TableRow>    
    <Button
        android:id="@+id/countbutton"
        android:text="@string/plus1"/>      
    <Button
        android:id="@+id/resetbutton"
        android:text="@string/reset" 
        />  
</TableRow>
</TableLayout>
Run Code Online (Sandbox Code Playgroud)

现在我得到的东西 - 一行的宽度和按钮的宽度取决于按钮内的TEXT.如果这两个文本都是等长的,那就说:TEXT它没关系 - 表格的一半位于屏幕中间.但如果它们有不同的尺寸 - 让我们说"A"和"这就是长按钮",桌子的中心不再在屏幕中间,所以按钮宽度不一样......

Rob*_*ond 90

要在按钮大小相同的行中设置按钮,您需要执行此操作.

    <LinearLayout android:orientation="horizontal" 
         android:layout_width="fill_parent"
         android:layout_height="fill_parent">
         <Button android:layout_weight="1" 
             android:layout_height="wrap_content" 
             android:layout_width="0dip"/>
         <Button android:layout_weight="1" 
             android:layout_height="wrap_content" 
             android:layout_width="0dip"/>
    </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

并填写按钮的其他xml属性.

神奇的是layout_weight和width属性.您不需要表格布局.这些属性告诉布局您的视图应占用父布局中的相等空间.


Uro*_*ric 6

很好的例子(最初来自http://androidadvice.blogspot.com/2010/10/tablelayout-columns-equal-width.html

已测试并工作:

<TableRow>
  <!-- Column 1 -->
  <TextView
     android:id="@+id/tbl_txt1"
     android:layout_width="0dip"
     android:layout_height="wrap_content"
     android:background="@color/red"
     android:textColor="@color/white"
     android:padding="10dip"
     android:layout_margin="4dip"
     android:layout_weight="1"
     android:text="Column 1" />

  <!-- Column 2 -->
  <TextView
     android:id="@+id/tbl_txt2"
     android:layout_width="0dip"
     android:layout_height="wrap_content"
     android:background="@color/red"
     android:textColor="@color/white"
     android:padding="10dip"
     android:layout_margin="4dip"
     android:layout_weight="1"
     android:text="Column 2" />

  <!-- Column 3 -->
  <TextView
     android:id="@+id/tbl_txt3"
     android:layout_width="0dip"
     android:layout_height="wrap_content"
     android:background="@color/red"
     android:textColor="@color/white"
     android:padding="10dip"
     android:layout_margin="4dip"
     android:layout_weight="1"
     android:text="Column 3" />
</TableRow>
Run Code Online (Sandbox Code Playgroud)