[英]How can I get an Android TableLayout to fill the screen?
我正在与Android糟糕的布局系统作斗争。 我正试图让桌子填满屏幕(简单吧?)但是这太难了。
我让它以某种方式在XML中工作,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent">
<TableRow android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_weight="1">
<Button android:text="A" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/>
<Button android:text="B" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/>
</TableRow>
<TableRow android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_weight="1">
<Button android:text="C" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/>
<Button android:text="D" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/>
</TableRow>
但是我不能让它在Java中工作。 我已经尝试了一百万种LayoutParams组合,但没有任何效果。 这是我最好的结果,它只填充屏幕的宽度,而不是高度:
table = new TableLayout(this);
// Java. You suck.
TableLayout.LayoutParams lp = new TableLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
table.setLayoutParams(lp); // This line has no effect! WHYYYY?!
table.setStretchAllColumns(true);
for (int r = 0; r < 2; ++r)
{
TableRow row = new TableRow(this);
for (int c = 0; c < 2; ++c)
{
Button btn = new Button(this);
btn.setText("A");
row.addView(btn);
}
table.addView(row);
}
显然Android文档没有帮助。 有人有主意吗?
上面的讨论有两个错误。
可以通过指定TableLayout.LayoutParams
和TableRow.LayoutParams
并使用适当的构造函数以编程方式设置权重,例如
TableLayout.LayoutParams rowInTableLp = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1.0f);
窗口小部件必须具有其父窗口的LayoutParams
。 因此,行必须使用TableLayout.LayoutParams
。
这为您提供了以下初始代码的工作版本:
TableLayout table = new TableLayout(this);
// Java. You succeed!
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
table.setLayoutParams(lp);
table.setStretchAllColumns(true);
TableLayout.LayoutParams rowLp = new TableLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT,
1.0f);
TableRow.LayoutParams cellLp = new TableRow.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT,
1.0f);
for (int r = 0; r < 2; ++r)
{
TableRow row = new TableRow(this);
for (int c = 0; c < 2; ++c)
{
Button btn = new Button(this);
btn.setText("A");
row.addView(btn, cellLp);
}
table.addView(row, rowLp);
}
setContentView(table);
感谢Romain Guy对Android开发人员解决方案论坛的评论。
最后弄清楚如何做到这一点。 放弃TableLayout
并在垂直方向内使用水平LinearLayout
。 关键的关键是设定重量。 如果指定FILL_PARENT
但使用默认权重,则它不起作用:
LinearLayout buttonsView = new LinearLayout(this);
buttonsView.setOrientation(LinearLayout.VERTICAL);
for (int r = 0; r < 6; ++r)
{
LinearLayout row = new LinearLayout(this);
row.setOrientation(LinearLayout.HORIZONTAL);
for (int c = 0; c < 4; ++c)
{
Button btn = new Button(this);
btn.setText("A");
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
lp.weight = 1.0f;
row.addView(btn, lp);
}
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
lp.weight = 1.0f;
buttonsView.addView(row, lp);
}
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
setContentView(buttonsView, lp);
找到答案:显然是layout_weight使它工作,没有办法从Java设置它。 该死的。
你永远不会设置行或按钮布局参数,而在发布的xml中,你这样做..更改for循环的细节以设置行布局参数和按钮布局参数,而不是它应该给出与xml相同的结果。
要设置TableLayout LayoutParams,我们逻辑上期望使用TableLayout.LayoutParams类,但是您将收到一个强制转换错误,指出TableLayout.LayoutParams无法转换为FrameLayout.LayoutParams。
因此,如果要以编程方式设置TableLayout属性,则应使用FrameLayout.LayoutParams。 例如:
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(80, 0, 0, 0);
TableLayout tableLayout = (TableLayout) findViewById(R.id.header_detail);
tableLayout.setLayoutParams(layoutParams);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.