如何修复 visibility="gone" 导致 TableLayout 中的 stretchColumns 错误计算宽度?

How to fix visibility="gone" causing stretchColumns in TableLayout to miscalculate widths?

我在 Horizo​​ntalScrollView 中嵌入了一个 TableLayout。某些列已隐藏 (android:visibility="gone")。但是,即使我在 TableLayout 上有 android:stretchColumns="*",列也不会伸展到全宽。这是我得到的:

<-----------------Screen Width---------------->
<---------HorizontalScrollView Width---------->
<---------TableLayout Width------------------->
<---------TableRow Width---------------------->
<-Col1-Col2-Col3-Col4-Col5->

关于如何解决这个问题有什么想法吗?如果我从 XML 布局中物理删除 gone 列 (Col6->Col9),那么这将按预期呈现。但是,以某种方式将列设置为 gone 干扰了 stretchColumns。

        <HorizontalScrollView
            android:layout_width="match_parent"
            android:fillViewport="true"
            android:layout_height="wrap_content">
            <TableLayout
                android:stretchColumns="*"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TableRow 
                    android:layout_width="match_parent"
                    android:layout_height="35dp">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="35dp"
                        android:text="Col1"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="35dp"
                        android:text="Col2"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="35dp"
                        android:text="Col3"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="35dp"
                        android:text="Col4"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="35dp"
                        android:text="Col5"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="35dp"
                        android:visibility="gone"
                        android:text="Col6"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="35dp"
                        android:visibility="gone"
                        android:text="Col7"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="35dp"
                        android:visibility="gone"
                        android:text="Col8"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="35dp"
                        android:visibility="gone"
                        android:text="Col9"/>
                </TableRow>
            </TableLayout>
        </HorizontalScrollView>

提供 TextView android:layout_weight="1"。更好的是,因为您的每个 TextView 都具有相同的样式,只需在 values/styles.xml 文件中为它们创建一个样式,如下所示:

<style name="your_text_view_style">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_weight">1</item>
    <item name="android:layout_height">35dp</item>
</style>

而不是为每个视图设置宽度和高度,只需在布局中为它们提供这种样式 xml。

style="@style/your_text_view_style"