我怎样才能得到一个Android

我怎样才能得到一个Android

本文介绍了我怎样才能得到一个Android TableLayout来填充屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我争夺与Android的可怕的布局系统。我试图让一个表来填充屏幕(简单吗?),但它是可笑很难。

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.

我把它以某种方式合作,这样的XML:

I got it to work somehow in XML like this:

<?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中工作。我试过的L​​ayoutParams的万种组合,但从来都没有工作。这是最好的结果我有只填满屏幕的宽度,而不是高度:

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:

    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的文档没有任何帮助。任何人有什么想法?

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

推荐答案

终于摸索出如何做到这一点。放弃了TableLayout,只是使用水平的LinearLayout的内部垂直的。临界键是设置权重。如果指定FILL_PARENT但与默认权重这是行不通的:

Finally worked out how to do this. Gave up on TableLayout and just used horizontal LinearLayout's inside a vertical one. The critical key is to set the weight. If you specify FILL_PARENT but with the default weight it doesn't work:

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

这篇关于我怎样才能得到一个Android TableLayout来填充屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 05:31