在表格内垂直对齐表格行以覆盖整个区域

发布于 2025-01-06 11:43:28 字数 2929 浏览 0 评论 0原文

我有一个表格覆盖整个屏幕并包含可变数量的行。

我想知道如何垂直均匀地分布这些行,以便它们占据整个表的空间。

我尝试过layout_weight和weightSum,但它不起作用。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:stretchColumns="0,1,2"
        android:id="@+id/maintable"
        android:weightSum="3" >

        <TableRow 
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:layout_weight="1" >
            <TextView
                android:text="text 1_1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                />

            <TextView
                android:text="text 2_1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                />

            <TextView
                android:text="text 3_1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                />
        </TableRow>

        <TableRow 
            android:layout_width="fill_parent"
            android:layout_height="1dp" 
            android:layout_weight="1" >
            <TextView
                android:text="text 1_2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                />

            <TextView
                android:text="text 2_2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                />

            <TextView
                android:text="text 3_2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                />
        </TableRow>

        <TableRow 
            android:layout_width="fill_parent"
            android:layout_height="1dp" 
            android:layout_weight="1" >
            <TextView
                android:text="text 1_3"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                />

            <TextView
                android:text="text 2_3"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                />

            <TextView
                android:text="text 3_3"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                />
        </TableRow>

    </TableLayout>
</LinearLayout>

I have a table that covers-up entire screen and contains variable number of rows.

I would like to know how can i distribute these rows vertically uniformly, so that they take up entire tables' space.

I have tried layout_weight, and weightSum, but its not working.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:stretchColumns="0,1,2"
        android:id="@+id/maintable"
        android:weightSum="3" >

        <TableRow 
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:layout_weight="1" >
            <TextView
                android:text="text 1_1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                />

            <TextView
                android:text="text 2_1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                />

            <TextView
                android:text="text 3_1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                />
        </TableRow>

        <TableRow 
            android:layout_width="fill_parent"
            android:layout_height="1dp" 
            android:layout_weight="1" >
            <TextView
                android:text="text 1_2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                />

            <TextView
                android:text="text 2_2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                />

            <TextView
                android:text="text 3_2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                />
        </TableRow>

        <TableRow 
            android:layout_width="fill_parent"
            android:layout_height="1dp" 
            android:layout_weight="1" >
            <TextView
                android:text="text 1_3"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                />

            <TextView
                android:text="text 2_3"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                />

            <TextView
                android:text="text 3_3"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                />
        </TableRow>

    </TableLayout>
</LinearLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦过后 2025-01-13 11:43:28

您的代码看起来错误,因为您没有为垂直设置“填充父级”约束

    <TableRow 
        android:layout_width="fill_parent"
        android:layout_height="1dp" 
        android:layout_weight="1" >

应该是

    <TableRow 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:layout_weight="1" >

Your code looks wrong as your are not setting 'fill parent' constraint for the vertical
axis

    <TableRow 
        android:layout_width="fill_parent"
        android:layout_height="1dp" 
        android:layout_weight="1" >

should be

    <TableRow 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:layout_weight="1" >
那小子欠揍 2025-01-13 11:43:28

请检查下面的代码。这将用表格行统一覆盖整个屏幕

grid_layout.xml

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

</TableLayout>

grid_layout 用代码中的文本视图填充,如下

public class GridLayoutActivity extends Activity {
    private static final int NO_OF_ROWS = 5;
    private static final int NO_OF_COLUMS = 5;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_layout);
        TableLayout gridview = (TableLayout) findViewById(R.id.tableLayout);
        for (int rowCnt = 0 ; rowCnt < NO_OF_ROWS; rowCnt++) {
             TableRow row = new TableRow(this);
         TableLayout.LayoutParams tableLayoutLP = new TableLayout.LayoutParams();
         tableLayoutLP.weight = 1;
         row.setLayoutParams(tableLayoutLP);

        for (int column = 0; column < NO_OF_COLUMS; column++) {
        TextView tv = new TextView(this);
        TableRow.LayoutParams tableRowLP = new TableRow.LayoutParams();
        tableRowLP.setMargins(2, 2, 2, 2);
        tableRowLP.weight=1;
        tableRowLP.height = TableRow.LayoutParams.MATCH_PARENT;
        tv.setTextColor(Color.WHITE);
        tv.setGravity(Gravity.CENTER);
        tv.setBackgroundColor(Color.DKGRAY);
        tv.setLayoutParams(tableRowLP);

        tv.setText("YourText");//Add your text here
        row.addView(tv);
        }

        gridview.addView(row);
    }
}

}

Please check below code. This will cover entire screen with table rows uniformly

grid_layout.xml

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

</TableLayout>

grid_layout is filled with text views in code as below

public class GridLayoutActivity extends Activity {
    private static final int NO_OF_ROWS = 5;
    private static final int NO_OF_COLUMS = 5;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_layout);
        TableLayout gridview = (TableLayout) findViewById(R.id.tableLayout);
        for (int rowCnt = 0 ; rowCnt < NO_OF_ROWS; rowCnt++) {
             TableRow row = new TableRow(this);
         TableLayout.LayoutParams tableLayoutLP = new TableLayout.LayoutParams();
         tableLayoutLP.weight = 1;
         row.setLayoutParams(tableLayoutLP);

        for (int column = 0; column < NO_OF_COLUMS; column++) {
        TextView tv = new TextView(this);
        TableRow.LayoutParams tableRowLP = new TableRow.LayoutParams();
        tableRowLP.setMargins(2, 2, 2, 2);
        tableRowLP.weight=1;
        tableRowLP.height = TableRow.LayoutParams.MATCH_PARENT;
        tv.setTextColor(Color.WHITE);
        tv.setGravity(Gravity.CENTER);
        tv.setBackgroundColor(Color.DKGRAY);
        tv.setLayoutParams(tableRowLP);

        tv.setText("YourText");//Add your text here
        row.addView(tv);
        }

        gridview.addView(row);
    }
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文