我花了几个小时研究这个问题并查看了我无法解决的所有相关问题。
这是我的问题:
我有一个图像的网格视图。我需要3列(以匹配iOS版本和美学)。如果我将numColumns设置为3,则每行的图像行的顶部和底部之间会有很多额外的空间。如果我将宽度设置为自动调整,我总是得到2,它们看起来更好但更喜欢3列。
我错过了什么?
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top" >
<LinearLayout
android:id="@+id/llayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
>
<ImageView
android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#666666"
android:clickable="false"
android:contentDescription="@string/title_card_collection"
android:src="@drawable/sportscard2x" />
</LinearLayout>
<GridView
android:id="@+id/cardGrid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_above="@+id/llayout"
android:gravity="center"
android:horizontalSpacing="2dip"
android:numColumns="3"
android:stretchMode="columnWidth"
android:layout_alignParentBottom="true"
android:verticalSpacing="2dip" >
</GridView>
我希望我很容易忘记这件事。提前谢谢。
更新:添加了屏幕截图。问题是只显示了一行,如果向下滚动,则会看到其他3行,但两者之间存在巨大的空间。
答案 0 :(得分:3)
的&LT;编辑&gt; 强> 的
请在下面找到“提示3”。可以找到代码示例here。
的&LT; /编辑&gt; 强> 的
我认为问题在于您将layout_height
的{{1}}设置为<GridView>
。网格视图似乎无法正确计算它的总包裹高度,因此,它只显示一行。
您可以将wrap_content
设置为layout_height
(或任何其他固定高度,如match_parent
或其他),或者您可以尝试扩展120dp
Java对象并进行一些自己的计算(然后您需要在XML布局中使用自定义网格视图对象:GridView
)。
我确实需要强调的是,在API 11之前的Java代码中没有任何明确定义的方法来获取网格视图中的列数(您可能需要这样来计算多少您的数据适配器将产生的行)。 API {11}中引入了<com.package.to.MyCustomGridView>
。也没有找到行之间间距的正确方法(API 16中引入了getNumColumns
和getHorizontalSpacing()
方法。)
供参考:http://developer.android.com/reference/android/widget/GridView.html
提示1: 我没有测试过这个我的自我,但你可以尝试这样的事情:
getVerticalSpacing()
即。包括线性布局中的网格视图,并在布置图像视图后为其添加所有可用空间。
提示2: 您可以编写自己的列表视图。在索尼移动教程网站上似乎有一些好吃的东西(不,我没有受雇于索尼移动:-),不过;一个好的教程是一个很好的教程):http://developer.sonymobile.com/2010/05/20/android-tutorial-making-your-own-3d-list-part-1/
提示3:
您可以扩展Java <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
/>
<GridView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.0"
...
/>
</LinearLayout>
对象并覆盖GridView
方法,如下例所示。请记住在Android布局XML文件中引用您的扩展onMeasure
类(如GridView
)
<com.package.MyGridView android:layout_width="match_parent" android:layout_height="wrap_content" ... />
希望你会在这里找到一些灵感: - )
干杯, - dbm