使用Table Layout Android的GridView

时间:2015-02-25 13:14:41

标签: android gridview android-tablelayout

我基本上尝试使用TableLayout创建一个gridview类的东西。我得到水平的东西(2列)工作正常。但是每个项目都被削减,看起来只占用50dp高度而不是120dp高度,如table_row.xml中所述

    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>

1 个答案:

答案 0 :(得分:0)

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