TableRow占用太多空间

时间:2019-07-20 16:33:01

标签: android android-layout android-tablelayout

我正在尝试使用TableLayout制作网格,但是rowHeights并未按预期分布。我不能使用gridlayout,因为我希望稍后改变行高。

xml:

<?xml version="1.0" encoding="utf-8"?>
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <androidx.cardview.widget.CardView
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">

                <View
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="#f0f"/>

                <View
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="#ff0"/>

            </LinearLayout>
        </androidx.cardview.widget.CardView>
    </TableRow>



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

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#0ff">
        </TextView>

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#0f0">
        </TextView>
        </TableRow>


    </TableLayout>

第一行应该只占据桌面布局高度的一半,但几乎填满整个屏幕。将cardViews内容替换为:

    <ListView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:background="#f00"/>

给了我期望的间距。我在做什么错了?

1 个答案:

答案 0 :(得分:0)

尝试对android:layout_height="wrap_content"CardViewLinearLayout使用View

相关问题