2016-11-28 20 views
1

我需要在项目中使用带网格线的3列网格视图。所以,我有一个结果;如何设置响应式XML的3列网格线

这是注3的结果(5.7" 和版本 “4.4.2”); http://i.imgur.com/fkkKOWE.jpg

这是注5的结果(5.7" 和版本 “机器人M”); http://i.imgur.com/6JFlSdf.jpg

这里我的代码; 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> 

categories_row.xml

<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> 

那么,如何获得像 “注3” 的结果,在所有的屏幕分辨率?

+0

检查我发布的代码。它会为你工作。 –

回答

0

您应该创建不同dimens个XML为不同的屏幕和设置文件categories_row.xml

... 
<RelativeLayout 
     android:layout_width="@dimen/box_width" 
     android:layout_height="@dimen/box_height" 
     android:background="#fff"> 
... 

你的RelativeLayout视图布局宽度和高度,你dimens.xml做笔记3将是这样的

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

然后创建另一个dimens.xml不同尺寸

+0

你好@gmetax,所以如果我为1080x1920使用120dp,我如何计算不同的屏幕尺寸?我没有不同的屏幕分辨率的设备,我不使用模拟器。 –

+0

来“看”你不需要一个模拟器,但只是布局查看器,你可以设置偏好大小 – gmetax

0

如果你将结合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" />