我想有一个2x2网格内的按钮。这只是ICS,所以我试图使用新的GridLayout给定。
这是我的布局的XML:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/favorites_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ff00"
android:rowCount="2"
android:columnCount="2">
<Button
android:text="Cell 0"
android:layout_row="0"
android:layout_column="0"
android:textSize="14dip" />
<Button
android:text="Cell 1"
android:layout_row="0"
android:layout_column="1"
android:textSize="14dip" />
<Button
android:text="Cell 2"
android:layout_row="1"
android:layout_column="0"
android:textSize="14dip" />
<Button
android:text="Cell 3"
android:layout_row="1"
android:layout_column="1"
android:textSize="14dip" />
</GridLayout>
问题是我的视图没有为每一行均匀地拉伸。这导致GridLayout的右侧有很多额外的空间。
我尝试设置layout_gravity="fill_horizontal",但这只适用于该行的最后一个视图。这意味着单元格1会一直延伸,为单元格0提供足够的空间。
如何解决这个问题?
更新:API 21支持权重。详见保罗的回答。
使用GridLayout时有一些限制,下面的引用摘自文档。
GridLayout不支持权重原则,因为
以权重定义。一般来说,因此不可能
配置GridLayout将多余的空间分布在非平凡中
多行或多列之间的比例…为了完全控制
超过多余的空间分布在行或列;使用线性布局
子视图来保存关联单元格组中的组件。
下面是一个使用LinearLayout子视图的小例子。(我使用空间视图占用未使用的区域,并将按钮推到所需的位置。)
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="1"
>
<TextView
android:text="2x2 button grid"
android:textSize="32dip"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" android:orientation="horizontal">
<Space
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<Space
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:text="Button 2" />
<Space
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Space
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3" />
<Space
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:text="Button 4" />
<Space
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
</GridLayout>
我只是在网格视图中做了简单的网格布局,并在想为什么我以前没有这样做。它很容易实现。(我在示例中使用自定义包装器CSView,但只是将其更改为View)。Ops要求只适用于GridView,所以我把它贴在这里作为答案。为了使拉伸均匀,以numColumns:auto_fit android:numColumns=2为例。
用法:
CSGridLayout(this, R.id.sound_store_grid, SubscriptionUpgradeStoreDialogItem(this),
PresetUpgradeStoreDialogItem(this), MidiUpgradeStoreDialogItem(this))
实现:
class CSGridLayout(parent: CSActivityView<*>,
viewId: Int,
vararg items: CSView<*>) : CSView<GridView>(parent, viewId) {
private val items = list(items)
private var adapter = Adapter()
init {
view.adapter = adapter
adapter.notifyDataSetChanged()
}
inner class Adapter : BaseAdapter() {
override fun getCount() = items.size
override fun getViewTypeCount() = 1
override fun isEnabled(position: Int) = true
override fun getItem(position: Int) = items[position]
override fun getItemViewType(position: Int) = 0
override fun getItemId(position: Int) = position.toLong()
override fun getView(position: Int, toReuseView: View?, parent: ViewGroup) =
items[position].view
}
}