我正在使用 Gridview,我想在其中显示一些图片。我希望我的网格视图在所有尺寸的屏幕上都看起来不错。我的意思是我正在为不同的 Android 设备开发我的应用程序,例如三星 Galaxy Grand(普通设备)、三星 Tab 4(7 英寸设备)和最终三星 Tab 10(10 英寸设备)。
因此,我希望我的网格视图采用自动编号,这在设备上看起来不错,例如我想要在三星平板电脑 10 英寸上显示 4 列,并在 7 英寸中具有相同的 3 或 2 列,在其他设备上也如此。
所以到目前为止我所做的很简单,为图像创建了一个数组并将适配器设置为 gridview,这是简单的代码,与我的问题无关,所以我不会共享该代码我所拥有的设计是在所以我的 xml 看起来像这样
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="250dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="50dp"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
</LinearLayout>
所以请帮我解决这个问题。如何使列数自动适应屏幕宽度。 ??
只需针对不同的屏幕尺寸使用不同的列数 - 覆盖不同的值values
文件夹(小、正常、大、超大),请参阅文档 http://developer.android.com/guide/topics/resources/providing-resources.html.
Example:
活动或片段:
<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="@integer/column_count"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:verticalSpacing="50dp"
/>
Item:
<ImageView
android:id="@+id/image"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
值/整数.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="column_count">2</integer>
</resources>
值-大/整数.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="column_count">3</integer>
</resources>
值-xlarge/integers.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="column_count">4</integer>
</resources>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)