RelativeLayout как TableLayout - PullRequest
       2

RelativeLayout как TableLayout

0 голосов
/ 05 августа 2011

Привет, есть RelativeLayout и хочу отобразить 6 кнопок, например «TableLayout», я рассчитал размер кнопки и попробовал отобразить на Activity. Появляется только последняя кнопка.

Я пытаюсь выполнить операцию с 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 голосов
/ 05 августа 2011

Если вы пытаетесь отобразить кнопки, равномерно распределенные по экрану, вам не нужно делать это в коде.Вы можете указать такие вещи в макете.

Что-то вроде этого даст 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 автоматически установит ширину равной, независимо от того, сколько у вас кнопок.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...