Android GridVIew根据方向更改列数 [英] Android GridVIew Change number of columns depending on Orientation
问题描述
我想以格子的形式显示6张图片,如下所示。
在纵向方向上,纵向,横向,横向和的
3列,2行
通过使用Android GridView并在layout-port和layout-land目录中定义不同的网格布局,我可以实现这种效果。
稍后按照我的活动要求,我在manifest.xml中添加了一个参数,它是
android:configChanges =mcc | mnc | locale | touchscreen | keyboard | keyboardHidden | navigation | orientation | fontScale | screenSize`
在屏幕方向改变时停止我的活动重新创建。
添加此参数后,我的网格视图不会按照预期的方式运行。它有时显示1列,有时2列,有时3列。
我将 gridView.setNumberOfColumns(2)
或 gridView.setNumberOfColumns(3)
方法在我的网格适配器的get view方法中,取决于设备的方向。
请在不删除Manifest.xml中的 android:configChanges
参数的情况下帮助我实现此效果
使用强大的资源系统。
在xml布局中,将列数设置为整数资源,然后在 /values/integers.xml
将它设置为2代表纵向,并在 /values-land/integers.xml
中设置为3 for landscape
//好吧,如果你在清单中做configChanges,你必须在onConfogurationChanged中改变java的列数。
I want to display 6 images in the form of grid as follows.
in portrait orientation,2 coumns, 3 rows and in landscare orientation 3 columns, 2 rows
By using Android GridView and by defining different grid layouts in layout-port and layout-land directories I was able to achieve this effect.
Later as per my activity requirement, I added one parameter in manifest.xml that is
android:configChanges = "mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|fontScale|screenSize"`
to stop my activity to recreate once screen orientation changes.
After adding this parameter, my grid view is not behaving in expected way. It sometimes shows 1 column, sometimes 2 columns, and sometimes 3 columns.
I am placing gridView.setNumberOfColumns(2)
or gridView.setNumberOfColumns(3)
methods in the get view method of my grid adapter depending on orientation of the device.
Please help me to achieve this effect without removing the android:configChanges
parameter in Manifest.xml
Use the powerful resource system.
In the xml layout, set the number of columns to a integer resource and then in /values/integers.xml
set it to 2 for portrait and in /values-land/integers.xml
set it to 3 for landscape
// well, if you do configChanges in manifest, you will have to change column count from java in onConfogurationChanged
这篇关于Android GridVIew根据方向更改列数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!