Android TableLayout表现不像表

时间:2017-07-05 12:05:13

标签: android

给定这个布局XML片段:

    <TableLayout
        android:id="@+id/review_ltpv_table"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:stretchColumns="*">

        <!-- TODO: This row's cells don't line up with the rows below. Fix. -->
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/review_text_view_look"
                android:text="@string/review_look"
                android:textAppearance="@style/TextBody" />

            <TextView
                android:id="@+id/review_text_view_taste"
                android:text="@string/review_taste"
                android:textAppearance="@style/TextBody" />

            <TextView
                android:id="@+id/review_text_view_portion"
                android:text="@string/review_portion"
                android:textAppearance="@style/TextBody" />

            <TextView
                android:id="@+id/review_text_view_value"
                android:text="@string/review_value"
                android:textAppearance="@style/TextBody" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="0dp" >

            <ToggleButton
                android:id="@+id/review_look_positive"
                style="@style/ButtonPositive"
                android:onClick="onLookPositive"
                android:textOn="@null"
                android:textOff="@null" />

            <ToggleButton
                android:id="@+id/review_taste_positive"
                style="@style/ButtonPositive"
                android:onClick="onTastePositive"
                android:textOn="@null"
                android:textOff="@null" />

            <ToggleButton
                android:id="@+id/review_portion_positive"
                style="@style/ButtonPositive"
                android:onClick="onPortionPositive"
                android:textOn="@null"
                android:textOff="@null" />

            <ToggleButton
                android:id="@+id/review_value_positive"
                style="@style/ButtonPositive"
                android:onClick="onValuePositive"
                android:textOn="@null"
                android:textOff="@null" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="0dp" >

            <ToggleButton
                android:id="@+id/review_look_negative"
                style="@style/ButtonNegative"
                android:onClick="onLookNegative"
                android:textOn="@null"
                android:textOff="@null" />

            <ToggleButton
                android:id="@+id/review_taste_negative"
                style="@style/ButtonNegative"
                android:onClick="onTasteNegative"
                android:textOn="@null"
                android:textOff="@null" />

            <ToggleButton
                android:id="@+id/review_portion_negative"
                style="@style/ButtonNegative"
                android:onClick="onPortionNegative"
                android:textOn="@null"
                android:textOff="@null" />

            <ToggleButton
                android:id="@+id/review_value_negative"
                style="@style/ButtonNegative"
                android:onClick="onValueNegative"
                android:textOn="@null"
                android:textOff="@null" />
        </TableRow>

    </TableLayout>

我明白这一点:

enter image description here

为什么顶行中的单元格之间的垂直边框与下面的行中的垂直边框不对齐?这不是一张桌子!

如果我从布局标记中删除android:stretchColumns="*",它看起来更糟糕 - 顶行中的单元格恰好在左侧。

如果我将android:layout_weight="1"添加到TextViews,则没有任何区别。

这是API级别22。

1 个答案:

答案 0 :(得分:0)

我认为你没有正确使用“重量”,我建议你尝试将你的第一行更改为:

    <TableRow
        android:weightSum="4">

        <TextView
            android:id="@+id/review_text_view_look"
            android:text="@string/review_look"
            android:layout_weight="1"
            android:textAppearance="@style/TextBody" />

        <TextView
            android:id="@+id/review_text_view_taste"
            android:text="@string/review_taste"
            android:layout_weight="1"
            android:textAppearance="@style/TextBody" />

        <TextView
            android:id="@+id/review_text_view_portion"
            android:text="@string/review_portion"
            android:layout_weight="1"
            android:textAppearance="@style/TextBody" />

        <TextView
            android:id="@+id/review_text_view_value"
            android:text="@string/review_value"
            android:layout_weight="1"
            android:textAppearance="@style/TextBody" />
    </TableRow>

所以每个细胞都具有相同的重量并具有相同的大小