28

我正在与 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 文档没有帮助。有人有想法么?

4

5 回答 5

35

上面的讨论有两个错误。

  1. 可以通过指定和使用适当的构造函数以编程方式设置权重TableLayout.LayoutParamsTableRow.LayoutParams例如

    TableLayout.LayoutParams rowInTableLp = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1.0f);
    
  2. 小部件必须具有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 开发者论坛上对解决方案的评论。

于 2011-01-10T12:32:35.373 回答
8

终于弄清楚了如何做到这一点。放弃了TableLayout,只是LinearLayout在垂直的里面使用了水平的 s。关键是设置权重。如果您指定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);
于 2010-03-20T00:36:16.337 回答
1

找到了答案:显然是 layout_weight 使它工作并且没有办法从 Java 中设置它。该死的。

请参阅如何获取 Android TableLayout 以在横向模式下填充父级?

于 2010-03-08T11:24:40.427 回答
0

您永远不会设置行或按钮布局参数,而在发布的 xml 中您会这样做..更改 for 循环的详细信息以设置行布局参数和按钮布局参数,而不是它应该给出与您的 xml 相同的结果。

于 2010-03-06T20:35:45.777 回答
0

要设置 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);
于 2016-11-04T09:07:04.137 回答