我正在关注 GridLayout http://blog.stylingandroid.com/archives/669上的本教程。我已经实现了如下代码:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="4"
android:rowCount="4">
<TextView
android:text="1,1"
android:gravity="center_horizontal"/>
<TextView
android:text="1,2"
android:gravity="center_horizontal"/>
<TextView
android:text="1,3"
android:layout_gravity="fill_horizontal"
android:gravity="center_horizontal"/>
<TextView
android:text="1,4"
android:gravity="center_horizontal"/>
<TextView
android:text="2,1"
android:gravity="center_horizontal"/>
<TextView
android:text="2,2"
android:gravity="center_horizontal"
android:layout_gravity="center"/>
<TextView
android:text="2,3"
android:layout_gravity="fill_horizontal"
android:gravity="center_horizontal"/>
<TextView
android:text="2,4"
android:gravity="center_horizontal"/>
<TextView
android:text="3,1"
android:gravity="center_horizontal"/>
<TextView
android:text="3,2"/>
<TextView
android:text="3,3 longer"
android:layout_gravity="fill_horizontal"
android:gravity="center_horizontal"/>
<TextView
android:text="3,4"
android:gravity="center_horizontal"/>
<TextView
android:text="4,1"
android:gravity="center_horizontal"/>
<TextView
android:text="4,2"
android:gravity="center_horizontal"/>
<TextView
android:text="4,3"
android:layout_gravity="fill_horizontal"
android:gravity="center_horizontal"/>
<TextView
android:text="4,4"
android:gravity="center_horizontal"/>
</GridLayout>
这产生了这个网格:
如果我了解它fill_horizontal
的工作原理,它不会使第 3 列尽可能大以适合文本,而是使第 3 列尽可能大(因为 gridlayout layout_width 是 match_parent)。
当我将一些较长的文本添加到另一列(比如 2,2)时,一切都会改变。
<TextView
android:text="1,1"
android:gravity="center_horizontal"/>
<TextView
android:text="1,2"
android:gravity="center_horizontal"/>
<TextView
android:text="1,3"
android:layout_gravity="fill_horizontal"
android:gravity="center_horizontal"/>
<TextView
android:text="1,4"
android:gravity="center_horizontal"/>
<TextView
android:text="2,1"
android:gravity="center_horizontal"/>
<TextView
android:text="2,2 longer, also"
android:gravity="center_horizontal"
android:layout_gravity="center"/>
<TextView
android:text="2,3"
android:layout_gravity="fill_horizontal"
android:gravity="center_horizontal"/>
<TextView
android:text="2,4"
android:gravity="center_horizontal"/>
<TextView
android:text="3,1"
android:gravity="center_horizontal"/>
<TextView
android:text="3,2"/>
<TextView
android:text="3,3 longer"
android:layout_gravity="fill_horizontal"
android:gravity="center_horizontal"/>
<TextView
android:text="3,4"
android:gravity="center_horizontal"/>
<TextView
android:text="4,1"
android:gravity="center_horizontal"/>
<TextView
android:text="4,2"
android:gravity="center_horizontal"/>
<TextView
android:text="4,3"
android:layout_gravity="fill_horizontal"
android:gravity="center_horizontal"/>
<TextView
android:text="4,4"
android:gravity="center_horizontal"/>
产生:
那么发生了什么事来改变事情呢?