如何让TableLayout垂直占用所有可用空间?

时间:2016-01-13 17:56:16

标签: java android textview tablelayout

我动态创建了一块电路板,并与Chronometer和Button一起放入了LinearLayout(Vertical)。我为每个人给出了以下权重:

董事会:5 天文台:10 按钮:10

天文台按钮并取正确位置,但是电路板不会占用您身高的所有空间。有人帮帮我吗?

Java类:



tableLayout = (TableLayout) findViewById(R.id.tabuleiro);

        GradientDrawable gd = new GradientDrawable();
        gd.setColor(azul);
        gd.setStroke(1, preto);

        final GradientDrawable gd2 = new GradientDrawable();
        gd2.setStroke(1, 0xFF000000);
        gd2.setColor(Color.RED);

        TableLayout.LayoutParams tableParams =  new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT);
        TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT);

        for (int i = 0; i < 10; i++){

            tableRow = new TableRow(this);
            tableRow.setLayoutParams(rowParams);

            for (int j = 0; j < 10; j++){

                textView = new TextView(this);
                textView.setLayoutParams(rowParams);
                textView.setBackgroundDrawable(gd);
                textView.setTag(String.valueOf(i + "." + j));

                textView.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        v.setBackgroundDrawable(gd2);
                    }
                });

                tableRow.addView(textView, rowParams);
            }

            tableLayout.addView(tableRow, tableParams);
        }
&#13;
&#13;
&#13;

XML:

&#13;
&#13;
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/lnl"
    android:background="#a9c3ff"
    android:orientation="vertical">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:gravity="center_vertical|center_horizontal"
        android:layout_marginTop="40dp"
        android:id="@+id/linearLayout2">

        <TableLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:stretchColumns="*"
            android:id="@+id/tabuleiro"
            android:layout_gravity="center_vertical"
            android:layout_weight="5">

        </TableLayout>

        <Chronometer
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/chrono"
            android:layout_gravity="center_horizontal"
            android:textSize="40dp"
            android:layout_weight="10"
            android:gravity="center" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/btn_voltar"
            android:id="@+id/btn_voltar"
            android:hyphenationFrequency="normal"
            android:layout_weight="10" />

    </LinearLayout>

</LinearLayout>
&#13;
&#13;
&#13;

IMAGE OF APP

1 个答案:

答案 0 :(得分:0)

尝试以下方法:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/lnl"
    android:background="#a9c3ff"
    android:orientation="vertical">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:gravity="center_vertical|center_horizontal"
        android:layout_marginTop="40dp"
        android:id="@+id/linearLayout2">

        <TableLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:stretchColumns="*"
            android:id="@+id/tabuleiro"
            android:layout_gravity="center_vertical"
            android:layout_weight="5">

        </TableLayout>

        <Chronometer
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/chrono"
            android:layout_gravity="center_horizontal"
            android:textSize="40dp"
            android:layout_weight="10"
            android:gravity="center" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/btn_voltar"
            android:id="@+id/btn_voltar"
            android:hyphenationFrequency="normal"
            android:layout_weight="10" />

    </LinearLayout>

</LinearLayout>