GridView 使用 Table 布局 Android

GridView using Table Layout Android

我基本上是在尝试使用 TableLayout 创建类似 gridview 的东西。我让水平的东西(2 列)工作正常。但是每个项目都被剪切了,看起来只占用了 50dp 高度而不是 table_row.xml

中提到的 120dp 高度
    for(int i=0;i<10;i++) {
                final TableRow tableRow = new TableRow(MyActivity.this);
                LayoutInflater inflater = LayoutInflater.from(MyActivity.this);
                final LinearLayout tableRowLayout = (LinearLayout) inflater.inflate(R.layout.table_row, null);

                tableRow.addView(tableRowLayout);

             tableLayout.addView(tableRow);
            }

我的table_layout.xml

    <TableLayout
                android:layout_margin="8dp"
                android:id="@+id/myTableLayout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:stretchColumns="0">

                <TableRow
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:id="@+id/myTableRow"
                    android:background="#00000000"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"></TableRow>
            </TableLayout>

我的table_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:weightSum="2"
    android:layout_height="120dp">
    <LinearLayout
        android:layout_weight="1"
        android:orientation="vertical"
        android:layout_width="0dp"
        android:weightSum="5"
        android:layout_height="match_parent">
        <ImageView
            android:layout_weight="4"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:src="@drawable/ic_launcher"
            android:id="@+id/imageone"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:textColor="@color/textColor"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="Text"
            android:layout_height="0dp" />

    </LinearLayout>
    <LinearLayout
        android:layout_weight="1"
        android:orientation="vertical"
        android:layout_width="0dp"
        android:weightSum="5"
        android:layout_height="match_parent">
        <ImageView
            android:layout_weight="4"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:src="@drawable/sofa"
            android:id="@+id/twoImage"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:textColor="@color/textColor"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="Text"
            android:layout_height="0dp" />

    </LinearLayout>

</LinearLayout>

我自己找到了解决方案。嵌套权重是导致此问题的原因。我们通常会忽略有关嵌套权重导致性能问题的警告,甚至会导致 UI 问题。

我像这样修改了我的 table_row.xml,它起作用了

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:weightSum="2"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_margin="4dp"
        android:layout_weight="1"
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="match_parent">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:src="@drawable/ic_launcher"
            android:id="@+id/OneImage"/>
        <TextView
            android:layout_width="match_parent"
            android:textColor="@color/textColor"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="Text"
            android:layout_height="wrap_content" />

    </LinearLayout>
    <LinearLayout
        android:layout_margin="4dp"
        android:layout_weight="1"
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="match_parent">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:src="@drawable/ic_launcher"
            android:id="@+id/TwoImage"/>
        <TextView
            android:layout_width="match_parent"
            android:textColor="@color/textColor"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="Text"
            android:layout_height="wrap_content" />

    </LinearLayout>

</LinearLayout>