2016-11-28 74 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" />