2012-04-17 23 views
1

GridViewに表示されているフラグメント間の境界線を取得しようとしています。 GridViewの背景色を白に、その断片の背景色を黒に設定しました。 はその後、私はそれらのコード行を挿入:Android GridViewの垂直の枠線

int border = 2; // px 
view.setColumnWidth((dm.widthPixels/2)-(4*border)); 
view.setNumColumns(2); 
view.setHorizontalSpacing(border); 
view.setVerticalSpacing(border); 

viewは私GridViewです。

結果は、GridView全体の左側と上端と下端の断片の間の境界線がきれいです。 しかし、上の両方のフラグメントの間には境界線がありません。ここで

GridViewの私の宣言です:

<?xml version="1.0" encoding="utf-8"?> 
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/overview" 
    android:stretchMode="columnWidth" 
    android:clipChildren="true" > 

</GridView> 

は、誰かが助けることができるかもしれませ。

が、私は私の以前のコメントで説明しようとしていたものを

+0

グリッドアイテムのレイアウトの背景として、いくつかのパディングまたは(9patch)ドロアブルを設定することはできますか? –

+0

私はあなたのヒントを完全に理解していません。あなたは何を意味するのか説明できますか? – Henrik

+0

@Henrik:私は答えとして私の説明を追加しました。それは数行に収まらないからです。 :)ところで、次にコメントに返信すると、その人にあなたの返答が通知されるので、 '@'(たとえば '@ MH.')を追加してください。 –

答えて

5

まあし、挨拶:GridViewが希望の間隔を調整することの代わりに、なぜあなたは、グリッド項目は、そのの世話を作るしようとしないのですか?このための簡単な解決策の1つは、GridViewのアダプタに/使用しているレイアウトファイルの背景として適切なドロウアブルを設定することです。つまり、あなたのグリッドアイテムに使用するレイアウトです。あなたが言及した断片。

簡単な例:

GridViewを取る:

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@android:color/white" 
    android:numColumns="4" /> 

そしてその中のアイテムに使用するレイアウト:

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/grid_background" 
    android:gravity="center" 
    android:text="Text" 
    android:textColor="@android:color/white" /> 

そして最後に、Aから魔法を背景ドロワブル:

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android">  
    <stroke android:width="5dp" android:color="@android:color/white" />  
    <padding android:bottom="5dp" android:left="5dp" android:right="5dp" 
     android:top="5dp" />  
    <solid android:color="@android:color/black" />  
</shape> 

結果は次のようになります。

gridview with 'borders'

私は、水平および垂直間隔パラメータは道によって、あなたのために動作しない理由はわかりません。

GridView::私はそれらを使用すると、私は上の画像のものとかなり類似した効果を得ることができるよ

<?xml version="1.0" encoding="utf-8"?> 
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@android:color/white" 
    android:horizontalSpacing="10dp" 
    android:numColumns="4" 
    android:verticalSpacing="10dp" /> 

アイテムのレイアウト(単に固定色の以前の背景リソースをスワップアウト) :

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:color/black" 
    android:gravity="center" 
    android:text="Text" 
    android:textColor="@android:color/white" /> 

結果:

another gridview with 'borders'

あなたのお手伝いをしますように。

+0

私は両方のソリューションを試しましたが、どちらも機能しませんでした。 "android:background =" @ drawable/grid_layout ""ステートメントをフラグメントの外側LinearLayoutに挿入しました。それは正しい? – Henrik

+0

申し訳ありませんが、grid_layoutのソリューションは正常に動作します。背景色を設定したフラグメントコードの行が見つかりました。この行を削除すると、すべてが完璧です。ありがとう!! – Henrik

+0

アダプタを使用してTextViewを設定するとどうなりますか?私はTextViewのXMLも入れなければなりませんか? –