网格根据屏幕大小查看自动列数

Coa*_*key 4 layout android gridview

我正在使用Gridview,我想在其中显示一些图片.我希望我的网格视图在各种尺寸的屏幕上看起来都很好.我的意思是我正在为不同的Android设备开发我的应用程序,例如三星Galaxy Grand是普通设备,三星标签4是7英寸设备,最后三星标签10是10英寸设备.

所以我希望我的网格视图能够获取在设备上看起来不错的自动编号,例如我想在三星平板电脑10英寸上使用4列,在7英寸中使用相同的比例3或2,在其他设备上也是如此.

所以到目前为止我所做的很简单,为图像创建了一个数组,并将适配器设置为gridview,这是简单的代码,与我的问题无关,所以我不分享那些代码我的设计是什么xml所以我的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>
Run Code Online (Sandbox Code Playgroud)

所以请帮我解决这个问题.如何使列数自动适应屏幕宽度.??

Ole*_*ndr 6

只需为不同的屏幕尺寸使用不同数量的列 - 覆盖不同values文件夹中的值(小,普通,大,xlarge),请参阅文档.

例:

活动或片段:

<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"
    />
Run Code Online (Sandbox Code Playgroud)

项目:

<ImageView
    android:id="@+id/image"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />
Run Code Online (Sandbox Code Playgroud)

值/ integers.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="column_count">2</integer>
</resources>
Run Code Online (Sandbox Code Playgroud)

值-大/ integers.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="column_count">3</integer>
</resources>
Run Code Online (Sandbox Code Playgroud)

值-XLARGE/integers.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="column_count">4</integer>
</resources>
Run Code Online (Sandbox Code Playgroud)