【问题标题】:Android: How do I set column width for unequal quantities of buttons?Android:如何为不等数量的按钮设置列宽?
【发布时间】:2015-09-02 22:27:40
【问题描述】:
我有 3 列按钮,但这些列并不都包含相同数量的按钮。使所有按钮显示相同大小的最佳做法是什么?我不想明确指定按钮宽度,我怀疑添加“虚拟”按钮也是最好的。
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*"
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="0dip"
android:layout_weight="1"
android:text="Button 1"
android:id="@+id/button1" />
<Button
android:layout_width="0dip"
android:text="Button 2"
android:id="@+id/button2"
android:layout_weight="1" />
<Button
android:layout_width="0dip"
android:text="Button 3"
android:id="@+id/button3"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:text="Button 4"
android:id="@+id/button4" />
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:text="Button 5"
android:id="@+id/button5"
android:layout_column="3" />
</TableRow>
</TableLayout>
【问题讨论】:
标签:
android
width
tablelayout
【解决方案1】:
这更像是一道数学题。为了在可扩展的网格空间中将赔率 (3) 和偶数 (2) 居中,您总共需要一个六格布局。
这是解决方案(没有任何额外的视图并且不使用布局权重):
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:stretchColumns="*">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_column="0"
android:layout_span="2"
android:text="Button 1" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_column="2"
android:layout_span="2"
android:text="Button 2" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_column="4"
android:layout_span="2"
android:text="Button 3" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="match_parent"
android:id="@+id/button4"
android:layout_column="1"
android:layout_span="2"
android:text="Button 4" />
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_column="3"
android:layout_span="2"
android:text="Button 5" />
</TableRow>
</TableLayout>
【解决方案2】:
您可以像往常一样添加三个按钮并隐藏最后一个按钮,如下所示
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*"
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="0dip"
android:layout_weight="1"
android:text="Button 1"
android:id="@+id/button1" />
<Button
android:layout_width="0dip"
android:text="Button 2"
android:id="@+id/button2"
android:layout_weight="1" />
<Button
android:layout_width="0dip"
android:text="Button 3"
android:id="@+id/button3"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="0dip"
android:layout_weight="1"
android:text="Button 4"
android:id="@+id/button4" />
<Button
android:layout_width="0dip"
android:text="Button 5"
android:id="@+id/button5"
android:layout_weight="1" />
<Button
android:layout_width="0dip"
android:text="Button 6"
android:id="@+id/button6"
android:layout_weight="1"
android:visibility="invisible"/>
</TableRow>
</TableLayout>