Android 如何为响应XML设置3列网格线

Android 如何为响应XML设置3列网格线,android,xml,gridview,Android,Xml,Gridview,我需要在项目中使用带有网格线的3列gridview。所以,我有一个结果 这是注3结果(5.7“和版本“4.4.2”) 这是注5结果(5.7”和版本“安卓M”) 这是我的密码; fragment_homepage.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match

我需要在项目中使用带有网格线的3列gridview。所以,我有一个结果

这是注3结果(5.7“和版本“4.4.2”)

这是注5结果(5.7”和版本“安卓M”)

这是我的密码; fragment_homepage.xml

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical" android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="#fff">

   <RelativeLayout
   android:layout_width="match_parent"
   android:layout_height="match_parent">



    <GridView
            android:id="@+id/GVHomepage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:numColumns="3"
            android:verticalSpacing="1dp"
            android:horizontalSpacing="1dp"
            android:background="#edf2f8" />

    <ProgressBar
        android:id="@+id/PBLoading"
        style="?android:attr/progressBarStyleInverse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />
    </RelativeLayout>

   </LinearLayout>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

    <RelativeLayout
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:background="#fff">

        <TextView
            android:id="@+id/TVCategoryName"
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Yiyecek"
            android:textSize="16sp"
            android:textStyle="bold"
            android:textColor="#745f87"
            android:layout_centerHorizontal="true"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <ImageView
            android:id="@+id/IVCategoryPhoto"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:scaleType="fitCenter"
            android:layout_marginTop="10dp"
            android:layout_centerHorizontal="true"
            android:layout_below="@+id/TVCategoryName"
            android:src="@mipmap/ic_launcher"/>
    </RelativeLayout>

   </LinearLayout>

categories\u row.xml

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical" android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="#fff">

   <RelativeLayout
   android:layout_width="match_parent"
   android:layout_height="match_parent">



    <GridView
            android:id="@+id/GVHomepage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:numColumns="3"
            android:verticalSpacing="1dp"
            android:horizontalSpacing="1dp"
            android:background="#edf2f8" />

    <ProgressBar
        android:id="@+id/PBLoading"
        style="?android:attr/progressBarStyleInverse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />
    </RelativeLayout>

   </LinearLayout>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

    <RelativeLayout
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:background="#fff">

        <TextView
            android:id="@+id/TVCategoryName"
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Yiyecek"
            android:textSize="16sp"
            android:textStyle="bold"
            android:textColor="#745f87"
            android:layout_centerHorizontal="true"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <ImageView
            android:id="@+id/IVCategoryPhoto"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:scaleType="fitCenter"
            android:layout_marginTop="10dp"
            android:layout_centerHorizontal="true"
            android:layout_below="@+id/TVCategoryName"
            android:src="@mipmap/ic_launcher"/>
    </RelativeLayout>

   </LinearLayout>


那么,如何在所有屏幕分辨率下获得“Note 3”这样的结果呢?

您应该为不同的屏幕创建不同的xml,并设置文件
categories\u row.xml的相对视图的布局宽度和高度

...
<RelativeLayout
        android:layout_width="@dimen/box_width"
        android:layout_height="@dimen/box_height"
        android:background="#fff">
...
。。。
...
注释3的dimens.xml如下所示

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="box_width">120dp</dimen>
    <dimen name="box_height">120dp</dimen>
</resources>

120dp
120dp

然后为不同的大小创建另一个dimens.xml

您应该为不同的屏幕创建不同的xml,并设置文件
categories\u row.xml的相对视图的布局宽度和高度

...
<RelativeLayout
        android:layout_width="@dimen/box_width"
        android:layout_height="@dimen/box_height"
        android:background="#fff">
...
。。。
...
注释3的dimens.xml如下所示

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="box_width">120dp</dimen>
    <dimen name="box_height">120dp</dimen>
</resources>

120dp
120dp

然后,如果您将android:numColumns=“auto_-fit”与android:stretchMode=“columnWidth”结合使用,则可以为不同的大小创建另一个dimens.xml。比固定列数要好得多。见下文

<GridView
            android:id="@+id/GVHomepage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:numColumns="3" // you can also use auto_fit here to set according to screens
            android:verticalSpacing="1dp"
            android:horizontalSpacing="1dp"
            android:stretchMode="columnWidth"
            android:background="#edf2f8" />

如果您将android:numColumns=“auto_-fit”与android:stretchMode=“columnWidth”相结合,将为您提供可根据不同屏幕大小进行调整的布局。比固定列数要好得多。见下文

<GridView
            android:id="@+id/GVHomepage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:numColumns="3" // you can also use auto_fit here to set according to screens
            android:verticalSpacing="1dp"
            android:horizontalSpacing="1dp"
            android:stretchMode="columnWidth"
            android:background="#edf2f8" />


Hello@gmetax,如果我对1080x1920使用120dp,如何计算不同的屏幕大小?我没有具有不同屏幕分辨率的设备,也没有使用Emulator。要“查看”,您不需要模拟器,只需要布局查看器,您可以设置首选大小hello@gmetax,因此如果我对1080x1920使用120dp,如何计算不同的屏幕大小?我没有不同屏幕分辨率的设备,也没有使用Emulator。要“查看”,您不需要模拟器,只需要布局查看器,您可以设置首选大小检查我发布的代码。它会为你工作。检查我张贴的代码。这对你有用。