2017-11-29 8 views
0

私は単純なアプリケーションを作ることに真剣に取り組んでいます.6つのカードビューを2つの列と3つの行に並べる必要があります。 グリッドレイアウトで正しいとわかっている場合は、画面の幅のちょうど半分を占める2つの列があり、高さを3等分する3つの行があります。 私が望むのは、gridlayoutのすべてのセルで、カードビューが中央に配置されていることです。 layout_gravity="center"のようなソリューションを試しましたが、カードビューはまだ画面の左側に残っています。どうすればいいですか?GridLayoutの2つのビューの中央揃え

これは私のXMLコードです:

<?xml version="1.0" encoding="utf-8"?> 
<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_height="match_parent" 
android:layout_width="match_parent" 
app:columnCount="2" 
app:rowCount="3" 
android:layout_gravity="center"> 

<android.support.v7.widget.CardView android:id="@+id/card1" 
    android:layout_height="@dimen/grid_card" 
    android:layout_width="@dimen/grid_card" 
    app:layout_column="0" 
    app:layout_row="0" /> 

<android.support.v7.widget.CardView android:id="@+id/card2" 
    android:layout_height="@dimen/grid_card" 
    android:layout_width="@dimen/grid_card" 
    app:layout_column="1" 
    app:layout_row="0" /> 

<android.support.v7.widget.CardView android:id="@+id/card3" 
    android:layout_height="@dimen/grid_card" 
    android:layout_width="@dimen/grid_card" 
    app:layout_column="0" 
    app:layout_row="1" /> 

<android.support.v7.widget.CardView android:id="@+id/card4" 
    android:layout_height="@dimen/grid_card" 
    android:layout_width="@dimen/grid_card" 
    app:layout_column="1" 
    app:layout_row="1" /> 

<android.support.v7.widget.CardView android:id="@+id/card5" 
    android:layout_height="@dimen/grid_card" 
    android:layout_width="@dimen/grid_card" 
    app:layout_column="0" 
    app:layout_row="2" /> 

<android.support.v7.widget.CardView android:id="@+id/card6" 
    android:layout_height="@dimen/grid_card" 
    android:layout_width="@dimen/grid_card" 
    app:layout_column="1" 
    app:layout_row="2"/> 


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

答えて

1

はあなたの<CardView>タグのそれぞれにこれらの3つの属性を追加します。

app:layout_gravity="center" 
app:layout_columnWeight="1" 
app:layout_rowWeight="1" 

重量attrsには、グリッド内の各セルは6分の1を占めていることを確認します重力attrは、カードがセル内の中央に配置されていることを確認します。

関連する問題