Java 如何让 Android TableLayout 填满屏幕?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2393847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:56:40  来源:igfitidea点击:

How can I get an Android TableLayout to fill the screen?

javaxmlandroidlayouttablelayout

提问by Timmmm

I'm battling with Android's awful layout system. I'm trying to get a table to fill the screen (simple right?) but it's ridiculously hard.

我正在与 Android 糟糕的布局系统作斗争。我想找一张桌子来填满屏幕(很简单吧?),但这太难了。

I got it to work somehow in XML like this:

我让它以某种方式在 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>

However I can not get it to work in Java. I've tried a million combinations of the LayoutParams, but nothing ever works. This is the best result I have which only fills the width of the screen, not the height:

但是我不能让它在 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);
    }

Obviously the Android documentation is no help. Anyone have any ideas?

显然,Android 文档没有帮助。谁有想法?

采纳答案by Timmmm

Finally worked out how to do this. Gave up on TableLayoutand just used horizontal LinearLayouts inside a vertical one. The critical key is to set the weight. If you specify FILL_PARENTbut with the default weight, it doesn't work:

终于想出了如何做到这一点。放弃了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);

回答by Fred Grott

You never set the row or button layout parameters whereas in the posted xml you do that..change the details of the for loops to set both the row layout parameters and the button layout parameters than it should give the same result as your xml.

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

回答by Timmmm

Found the answer: apparently it is the layout_weight that makes it work and there is no way to set it from Java. Damnit.

找到答案:显然是 layout_weight 使它起作用,并且无法从 Java 设置它。该死的。

See How can I get an Android TableLayout to fill the parent in landscape mode?

请参阅如何让 Android TableLayout 以横向模式填充父级?

回答by Martin

There are two mistakes in the above discussion.

上面的讨论有两个错误。

  1. It is possible to programatically set the weight by specifying TableLayout.LayoutParamsand TableRow.LayoutParamsand using the appropriate constructor, e.g.

    TableLayout.LayoutParams rowInTableLp = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1.0f);
    
  2. A widget must have the LayoutParamsof its parent. Therefore, the rows must use TableLayout.LayoutParams.

  1. 有可能通过指定编程方式设置的权重TableLayout.LayoutParams,并TableRow.LayoutParams和使用合适的构造,例如

    TableLayout.LayoutParams rowInTableLp = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1.0f);
    
  2. 小部件必须具有LayoutParams其父级的 。因此,行必须使用TableLayout.LayoutParams.

This gives you the following working version of your initial code:

这为您提供了初始代码的以下工作版本:

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);

Thanks to Romain Guy's comment on Android developer's forum for the solution.

感谢 Romain Guy 在 Android 开发者论坛上对解决方案的评论。

回答by Mohammed Mukhtar

To set TableLayout LayoutParams, we logically expect to use TableLayout.LayoutParams class, but you will get a cast error stating that TableLayout.LayoutParams cannot be casted into FrameLayout.LayoutParams.

要设置 TableLayout LayoutParams,我们在逻辑上希望使用 TableLayout.LayoutParams 类,但是您将收到一个转换错误,指出 TableLayout.LayoutParams 不能转换为 FrameLayout.LayoutParams。

So, you should use FrameLayout.LayoutParams, if you want to programmatically set TableLayout properties. For example:

因此,如果您想以编程方式设置 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);