如何在java中以编程方式使用按钮进行表格布局?
我叫帕维尔 我想要这样的布局: 我想用java做布局,因为按钮的数量会改变。正方形是按钮。 布局包括:如何在java中以编程方式使用按钮进行表格布局?,java,android,android-layout,button,android-tablelayout,Java,Android,Android Layout,Button,Android Tablelayout,我叫帕维尔 我想要这样的布局: 我想用java做布局,因为按钮的数量会改变。正方形是按钮。 布局包括: <LinearLayout> <Button/> <TableLayout> <TableRow> <Button1/> <Button2/> </TableRow> &
<LinearLayout>
<Button/>
<TableLayout>
<TableRow>
<Button1/>
<Button2/>
</TableRow>
<TableRow>
<Button3/>
<Button4/>
</TableRow>
<TableLayout>
<Button/>
</LinearLayout>
eclipse中的错误:
09-21 12:41:09.062:E/AndroidRuntime(758):java.lang.arithmetricException:被零除
我认为这与拉伸柱有关
我的问题是:如何在android中纠正这一点,使布局与图片中的一样?更改这一行
LinearLayout.LayoutParams buttonLayout =
new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.0f);
到
在拉伸所有字段时,我觉得没有必要使用权重参数。它与FILL_PARENT相同如何为TableLayout中的按钮创建布局?如果不将布局设置为TableLayout内的按钮,则不会出现错误。错误异常发生在哪一行09-21 15:15:13.285:E/AndroidRuntime(779):在android.widget.TableLayout.mutateColumnsWidth(TableLayout.java:583)尝试将
android:stretchColumns=“0,1”
添加到TableLayout
。。不要使用重量。为什么按钮的宽度为零?问题解决了。元素的布局应该具有父元素的布局。我还更改了其余部分,而不仅仅是这一行。
LinearLayout.LayoutParams buttonLayout =
new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.0f);
TableRow.LayoutParams buttonLayout =
new TableRow.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);