【发布时间】:2013-11-11 18:57:10
【问题描述】:
我有一个带有类似项目的自定义网格视图
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center"
android:longClickable="false"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:longClickable="false"
android:text="0"
android:textSize="60sp" />
</LinearLayout>
我希望我的项目是正方形,我希望 gridview 拉伸宽度以填充纵向屏幕的所有宽度和横向的所有高度。 它应该看起来像这样
其中 A - 是正方形的边,B 是边距宽度(可能为零)。 我认为我可能应该重写 onMeasure 方法,但我究竟应该怎么做? 也许有人可以帮忙?
编辑 好的,我尝试在适配器的getView方法中手动设置项目的宽度和高度,这更好,但仍然不是我想要的。我怎样才能摆脱列之间的间距?
【问题讨论】:
-
查看我关于如何管理 GridView stackoverflow.com/a/13835812/833622的答案
-
您的建议基本上是手动计算项目的高度和宽度,然后将此值传递给适配器,对吗?虽然这可能会奏效,但恕我直言,这是一个尴尬的解决方案。
-
实际上你只会在gridview被测量后才设置适配器。它与使用 GlobalLayoutListener 相同。一旦测量了gridview,您就可以准确计算所有内容。
-
我可能应该实现我自己的自定义适配器视图,它会有我想要的属性。不幸的是,这需要相对较长的时间,因为我以前没有这样做过。