Android中控件GridView實(shí)現(xiàn)設(shè)置行列分割線的方法示例
前言
雖然Aandroid目前已經(jīng)有RecyclerView了、非常強(qiáng)大的一個(gè)View、可以直接控制成ListView以及GridView等、而且畫框線也比較方便、但是呢在很多情況下我們不得不仍然使用GridView來(lái)實(shí)現(xiàn)布局、那么在這個(gè)時(shí)候我們又要怎么來(lái)對(duì)GridViw進(jìn)行畫框線呢、下面將提供兩種實(shí)現(xiàn)方式、大家可以選擇一下
一、設(shè)置垂直、橫向間距、通過(guò)GRIDVIEW和ITEM的背景色來(lái)實(shí)現(xiàn)
1、設(shè)置GridView背景色
2、設(shè)置水平和豎直方向間隔:android:horizontalSpacing
和android:verticalSpacing
3、設(shè)置GridView的item的背景色及其選中后的顏色
XML代碼如下
<GridView android:id="@ id/gridView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@ id/textView1" android:layout_marginTop="30dp" android:background="#999999" android:horizontalSpacing="0.5dp" android:verticalSpacing="0.5dp" android:padding="2dp" android:numColumns="3" >
item布局
<?xml version="1.0" encoding="utf-8"?> <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" android:background="@android:color/whith"> <TextView android:id="@ id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:padding="10dp" android:text="TextView" /> </LinearLayout>
二、設(shè)置選擇器
這種方式就是設(shè)置item的選擇器的android:background
屬性來(lái)實(shí)現(xiàn)的
gv_selector代碼
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" > <shape android:shape="rectangle"> <solid android:color="#CCCCCC" /> <stroke android:width="1.0px" android:color="#999999" /> </shape> </item> <item android:state_pressed="true" > <shape android:shape="rectangle"> <solid android:color="#CCCCCC" /> <stroke android:width="1.0px" android:color="#999999" /> </shape> </item> <item> <shape android:shape="rectangle"> <stroke android:width="1.0px" android:color="#999999" /> </shape> </item> </selector>
至此兩種方法已經(jīng)寫完了、有細(xì)心的朋友可能會(huì)發(fā)現(xiàn)第二種方法的中間的網(wǎng)格線比邊線要粗兩倍、這也是第二種方法的一個(gè)不足的地方
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流。
相關(guān)文章
Android實(shí)現(xiàn)EditText的富文本編輯
這篇文章主要介紹了Android實(shí)現(xiàn)EditText的富文本編輯,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Android WebView交互傳遞json字符串并解析的方法
這篇文章主要給大家介紹了關(guān)于Android中WebView交互傳遞json字符串并解析的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-05-05Android Activity Results API代替onActivityResul
說(shuō)到onActivityResult,我們已經(jīng)非常熟悉來(lái),通過(guò)在A activity啟動(dòng)B activity并且傳入數(shù)據(jù)到B中,然后在A中通過(guò)onActivityResult來(lái)接收B中返回的數(shù)據(jù)。在最新的activity-ktx的beta版本中,谷歌已經(jīng)廢棄了onActivityResult2022-09-09Android編程實(shí)現(xiàn)泡泡聊天界面實(shí)例詳解(附源碼)
這篇文章主要介紹了Android編程實(shí)現(xiàn)泡泡聊天界面,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android泡泡聊天界面的窗體定義與功能實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11解決Android Studio一直停留在MyApplication:syncing的問(wèn)題
這篇文章主要介紹了Android Studio一直停留在MyApplication:syncing的完美解決方案,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10完美解決客戶端webview持有的頁(yè)面緩存,不會(huì)立即釋放的問(wèn)題
下面小編就為大家?guī)?lái)一篇完美解決客戶端webview持有的頁(yè)面緩存,不會(huì)立即釋放的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12Android仿微信界面的導(dǎo)航以及右上角菜單欄效果
這篇文章主要介紹了Android仿微信界面的導(dǎo)航以及右上角菜單欄效果,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,一起跟隨小編過(guò)來(lái)看看吧2018-05-05Android studio 出現(xiàn)錯(cuò)誤Run with --stacktrace option to get the s
這篇文章主要介紹了 Android studio 出現(xiàn)錯(cuò)誤Run with --stacktrace option to get the stack trace. Run with --info or --debu的相關(guān)資料,需要的朋友可以參考下2016-11-11