【问题标题】:How do I get TableLayout occupy all available space vertically?如何让 TableLayout 垂直占据所有可用空间?
【发布时间】:2016-01-14 02:19:35
【问题描述】:

我动态创建了一个板,并将一个 LinearLayout ( Vertical ) 与一个 Chronometer 和一个 Button 放在一起。我为每个人赋予了以下权重:

棋盘: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);
        }

XML:

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

IMAGE OF APP

【问题讨论】:

    标签: java android textview tablelayout


    【解决方案1】:

    尝试以下方法:

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

    【讨论】: