开发者

RelativeLayout like TableLayout

Hi a have RelativeLayout and want display 6 buttons like a "TableLayout", i calc size buttons and tried display on Activity. Only last button appear.

I trying do Activity with 6 buttons (buttonsperscreen = 6) when call method.

Can you help me?

The code is:

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 iteratio开发者_开发问答n
            if (posx + widthButton < w) {
                posx = posx + widthButton;
            } else {
                posx = 0;
                posy = posy + heightButton;
            }

        }

    }

}

Thanks Mateus


If you're trying to display buttons evenly spaced across a screen, you don't need to do this in code. You can specify things like this in a layout.

Something like this will give 6 buttons equal widths displayed horizontally.

<?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>

As long as you keep the layout_width to be 0dp, and set the weight to be 1, Android will automatically set the widths to be equal, no matter what number of buttons you have.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜