Android中控件GridView實現(xiàn)設(shè)置行列分割線的方法示例
前言
雖然Aandroid目前已經(jīng)有RecyclerView了、非常強大的一個View、可以直接控制成ListView以及GridView等、而且畫框線也比較方便、但是呢在很多情況下我們不得不仍然使用GridView來實現(xiàn)布局、那么在這個時候我們又要怎么來對GridViw進行畫框線呢、下面將提供兩種實現(xiàn)方式、大家可以選擇一下
一、設(shè)置垂直、橫向間距、通過GRIDVIEW和ITEM的背景色來實現(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
屬性來實現(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)寫完了、有細心的朋友可能會發(fā)現(xiàn)第二種方法的中間的網(wǎng)格線比邊線要粗兩倍、這也是第二種方法的一個不足的地方
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
相關(guān)文章
Android WebView交互傳遞json字符串并解析的方法
這篇文章主要給大家介紹了關(guān)于Android中WebView交互傳遞json字符串并解析的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對各位Android開發(fā)者具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-05-05Android Activity Results API代替onActivityResul
說到onActivityResult,我們已經(jīng)非常熟悉來,通過在A activity啟動B activity并且傳入數(shù)據(jù)到B中,然后在A中通過onActivityResult來接收B中返回的數(shù)據(jù)。在最新的activity-ktx的beta版本中,谷歌已經(jīng)廢棄了onActivityResult2022-09-09Android編程實現(xiàn)泡泡聊天界面實例詳解(附源碼)
這篇文章主要介紹了Android編程實現(xiàn)泡泡聊天界面,結(jié)合實例形式較為詳細的分析了Android泡泡聊天界面的窗體定義與功能實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11解決Android Studio一直停留在MyApplication:syncing的問題
這篇文章主要介紹了Android Studio一直停留在MyApplication:syncing的完美解決方案,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10完美解決客戶端webview持有的頁面緩存,不會立即釋放的問題
下面小編就為大家?guī)硪黄昝澜鉀Q客戶端webview持有的頁面緩存,不會立即釋放的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12Android仿微信界面的導(dǎo)航以及右上角菜單欄效果
這篇文章主要介紹了Android仿微信界面的導(dǎo)航以及右上角菜單欄效果,具有很好的參考價值,希望對大家有所幫助,一起跟隨小編過來看看吧2018-05-05Android studio 出現(xiàn)錯誤Run with --stacktrace option to get the s
這篇文章主要介紹了 Android studio 出現(xiàn)錯誤Run with --stacktrace option to get the stack trace. Run with --info or --debu的相關(guān)資料,需要的朋友可以參考下2016-11-11