2012-04-17 124 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)drawable作爲你的網格物品佈局的背景嗎? – 2012-04-17 08:32:26

+0

我完全不明白你的提示。你能解釋一下你的意思嗎? – Henrik 2012-04-18 07:05:35

+0

@Henrik:我加了我的解釋作爲答案,因爲它不適合幾行。 :)順便說一下,下次您回覆評論時,請添加@ @ @ @ @ @ 0 @@(例如'@ MH.'),因爲那個人會收到您的回覆通知。 – 2012-04-18 22:09:45

回答

5

那麼,我想在我先前的評論來解釋:在具有GridView調節你想要的間距,你爲什麼不嘗試,使電網項目採取照顧代替?一個簡單的解決方案是通過設置一個合適的可繪製的背景作爲您使用/傳遞到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" /> 

最後,從魔術可繪製背景:

<?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 2012-04-19 07:28:18

+0

對不起,你的解決方案與grid_layout工作正常!我在我設置背景顏色的片段代碼中找到了一行。刪除這條線一切都很完美。謝謝!! – Henrik 2012-04-19 10:57:53

+0

如果我使用適配器來設置TextView,該怎麼辦?我應該把TextView的XML嗎? – 2013-05-02 10:35:25