RelativeLayout就像TableLayout一样

时间:2011-08-04 20:12:42

标签: android android-layout

你有一个RelativeLayout,并希望显示6个按钮,如“TableLayout”,我计算尺寸按钮,并试图在活动上显示。只显示最后一个按钮。

我在调用方法时尝试使用6个按钮(buttonsperscreen = 6)进行活动。

你能帮帮我吗?

代码是:

private void doLayoutWithButtons(int buttonsperscreen) {

    // if total butons is % 2 == 0
    if (buttonsperscreen % 2 == 0) {
        // get display dimensions
        DisplayMetrics d = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(d);

        int w = d.widthPixels;
        int h = d.heightPixels;

        // calc size of buttons
        int widthButton = w / 2; // only two columns of buttons.
        int heightButton = h / ((buttonsperscreen) / 2); // sample 100px /
                                                            // (6/2) = 3

        int posx = 0;
        int posy = 0;

        for (int i = 1; i <= buttonsperscreen; i++) {

            Button newButton = new Button(this);

            newButton.setId(100 + i + 1); // ID of zero will not work
            newButton.setText("Botão: " + i);

            // atribuindo o tamanho do botão.
            newButton.setHeight(heightButton);
            newButton.setWidth(widthButton);
            newButton.setId(1000 + i);

            // positioning buttons...
            RelativeLayout layout1 = new RelativeLayout(this);

            // set layout with size of button
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    widthButton, heightButton);
            params.leftMargin = posx;
            params.topMargin = posy;

            newButton.setLayoutParams(params);

            layout1.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
            layout1.addView(newButton);
            setContentView(layout1);

            // to calc positions x,y for each button for next iteration
            if (posx + widthButton < w) {
                posx = posx + widthButton;
            } else {
                posx = 0;
                posy = posy + heightButton;
            }

        }

    }

}

由于 马特乌斯

1 个答案:

答案 0 :(得分:0)

如果您尝试在屏幕上均匀显示按钮,则无需在代码中执行此操作。您可以在布局中指定这样的内容。

这样的东西会给6个按钮水平显示相等的宽度。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:orientation="horizontal">
        <Button android:layout_height="wrap_content" android:id="@+id/button1"
            android:text="Button" android:layout_width="0dp"
            android:layout_weight="1" />
        <Button android:layout_height="wrap_content" android:id="@+id/button2"
            android:text="Button" android:layout_width="0dp"
            android:layout_weight="1" />
    </LinearLayout>
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:orientation="horizontal">
        <Button android:layout_height="wrap_content" android:id="@+id/button3"
            android:text="Button" android:layout_width="0dp"
            android:layout_weight="1" />
        <Button android:layout_height="wrap_content" android:id="@+id/button4"
            android:text="Button" android:layout_width="0dp"
            android:layout_weight="1" />
    </LinearLayout>
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:orientation="horizontal">
        <Button android:layout_height="wrap_content" android:id="@+id/button5"
            android:text="Button" android:layout_width="0dp"
            android:layout_weight="1" />
        <Button android:layout_height="wrap_content" android:id="@+id/button6"
            android:text="Button" android:layout_width="0dp"
            android:layout_weight="1" />
    </LinearLayout>
</LinearLayout>

只要你将layout_width保持为0dp,并将权重设置为1,Android就会自动将宽度设置为相等,无论你有多少个按钮。

相关问题