代码之家  ›  专栏  ›  技术社区  ›  Chris Knight

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

  •  0
  • Chris Knight  · 技术社区  · 7 年前

    我有一个嵌入在水平滚动视图中的表格布局。有些列是隐藏的( android:visibility="gone" android:stretchColumns="*" 在TableLayout上,列不会延伸到整个宽度。我得到的是:

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

    有没有办法解决这个问题?如果我从XML布局中实际删除 gone 列(列6->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>
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Arash Rohani    7 年前

    给出文本视图 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>
    

    style="@style/your_text_view_style"