2016-05-11 55 views
-1

在我的學習中,我必須做一個Android應用程序,從氣象站中檢索天氣數據。這些將以塊形式顯示。 這些塊將在4列和2行。Square Grid - XML

enter image description here

所以我想創造4列和2線提供塊的方形網格。

有人會有一個解決方案來幫助我創建這個網格嗎?

回答

0

有很多選擇:1。 你可以選擇與GridLayoutManager

<android.support.v7.widget.RecyclerView 
    android:id="@+id/main_grid" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="2dp" 
    /> 

所以內部活動回收視圖,你會做這樣的事情

mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), COLUMN_COUNT)); 

2.您選擇網格視圖

<GridView 
android:id="@+id/grid" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:drawSelectorOnTop="true" 
android:horizontalSpacing="2dp" 
android:numColumns="4" 
android:padding="4dp" 
android:stretchMode="columnWidth"/> 

對於固定的項目大小,您應該使用

<android.support.v7.widget.GridLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:columnCount="4" 
    app:orientation="horizontal" 
    > 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Hello World!" 
    /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Hello World!" 
    /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Hello World!" 
    /> 
    <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Hello World!" 
    /> 

</android.support.v7.widget.GridLayout> 

確保在您的應用程序gradle這個你的答案添加此

compile 'com.android.support:gridlayout-v7:23.0.1' 
+0

謝謝!我更喜歡選擇XML,所以GridView。有了這個,我可以得到我的方格? – McNavy

+0

應該工作..請評論,如果有任何問題 – user3354265

+0

我想要一個網格,爲設計塊。讓我解釋。我想要一個第2行和第4列的網格。我也希望從網格中獲益,從而最初區分2行和4個塊。你有沒有解決我的問題的方法? – McNavy