【问题标题】:Android TableLayout not behaving like a tableAndroid TableLayout 的行为不像表格
【发布时间】:2017-07-05 20:49:00
【问题描述】:

鉴于此布局 XML sn-p:

    <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>

我看到了:

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

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

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

这是 API 级别 22。

【问题讨论】:

    标签: android


    【解决方案1】:

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

        <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>
    

    所以每个单元格的重量相同,大小也相同

    【讨论】:

    • 没什么区别,抱歉。如果这行得通,它也会提出一个问题:如果需要告知所有这些权重,TableLayout 的意义何在?
    • 尝试从每个 TableRow 中删除所有 layout_width 和 layout_height,我认为这些属性会改变 TableLayout 的行为
    • 不走运,抱歉。
    • 是的,没有区别。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-14
    • 1970-01-01
    • 2017-07-15
    • 1970-01-01
    • 1970-01-01
    • 2020-05-15
    相关资源
    最近更新 更多