Android應(yīng)用開發(fā)中使用GridView網(wǎng)格布局的代碼示例
基本布局演示
1. 定義包含GridView 的 main.xmk
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <GridView android:id="@+id/gride" android:layout_width="fill_parent" android:layout_height="wrap_content" <strong> android:numColumns="3"</strong> android:verticalSpacing="5dip" /> </LinearLayout>
這行應(yīng)該注意一下:
android:numColumns="3"
用來(lái)設(shè)定GridView每行顯示的View數(shù)目 如果沒(méi)有這行 會(huì)默認(rèn)每行顯示一個(gè)View 和ListView 的一樣
2. 自定義 class ImageList extends BaseAdapter 其中主要是:
View getView(int position, View convertView, ViewGroup parent)
用于顯示目標(biāo)ImageView
public class ImageList extends BaseAdapter {
Activity activity;
//construct
public ImageList(Activity a ) {
activity = a;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return image.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return image[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView iv = new ImageView(activity);
iv.setImageResource(image[position]);
return iv;
}
}
3. 給GridView指定Adapter
GridView gv = (GridView) findViewById(R.id.gride); ImageList adapter = new ImageList(this); gv.setAdapter(adapter);
所以最后效果圖是這樣的

巧妙地添加GridView的 網(wǎng)格線
ListView 中設(shè)置分隔線的加如下參數(shù)即可:
android:divider="@color/gray" android:dividerHeight="1dp"
GridView網(wǎng)格布局,默認(rèn)情況下是沒(méi)有網(wǎng)格線的
查找網(wǎng)上資料,找到了一種為GridView添加網(wǎng)格線的小技巧
實(shí)際上,該網(wǎng)格線是通過(guò)設(shè)置GridView各子項(xiàng)的間隔,并分別設(shè)置GridView背景色與子項(xiàng)背景色實(shí)現(xiàn)的。
實(shí)現(xiàn)方法:
(1)設(shè)置GridView背景色,設(shè)置水平間方向間隔屬性值android:horizontalSpacing和豎直方向間隔屬性值
android:verticalSpacing
(2)設(shè)置GridView子項(xiàng)背景色
示例代碼:
1.main.xml
<GridView
android:id="@+id/gv_words"
android:visibility="gone"
android:background="@color/gray"
android:columnWidth="60dp"
android:numColumns="5"
android:listSelector="@null"
android:verticalSpacing="1.0px"
android:horizontalSpacing="1.0px"
android:soundEffectsEnabled="true"
android:smoothScrollbar="true"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/llayout2"
android:layout_below="@+id/llayout1"/>
2.GrivViewItem布局
<?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="wrap_content"
android:background="@color/white" >
<RelativeLayout
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="5dp">
<TextView
android:id="@+id/gv_bushou_TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ItemImage"
android:layout_margin="1dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_above="@+id/gv_bushou_TextView2"
android:layout_toLeftOf="@+id/gv_bushou_TextView2"
android:textSize="25dp"
android:textColor="@color/blue"
android:text="難" >
</TextView>
<TextView
android:id="@+id/gv_bushou_TextView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ItemImage"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:textColor="@color/gray"
android:textSize="10dp"
android:text="1筆" >
</TextView>
</RelativeLayout>
</LinearLayout>
3.運(yùn)行截圖

- Android中使用GridLayout網(wǎng)格布局來(lái)制作簡(jiǎn)單的計(jì)算器App
- Android布局之GridLayout網(wǎng)格布局
- Android RecyclerView使用GridLayoutManager間距設(shè)置的方法
- Android使用GridLayout繪制自定義日歷控件
- Android自定義ViewGroup之CustomGridLayout(一)
- Android中RecyclerView布局代替GridView實(shí)現(xiàn)類似支付寶的界面
- Android中使用GridView進(jìn)行應(yīng)用程序UI布局的教程
- Android網(wǎng)格布局GridView實(shí)現(xiàn)漂亮的多選效果
- Android App中的GridView網(wǎng)格布局使用指南
- Android開發(fā)之計(jì)算器GridLayout布局實(shí)現(xiàn)方法示例
相關(guān)文章
Android?ViewPager2?+?Fragment?聯(lián)動(dòng)效果的實(shí)現(xiàn)思路
這篇文章主要介紹了Android?ViewPager2?+?Fragment?聯(lián)動(dòng),本篇主要介紹一下 ViewPager2 + Fragment聯(lián)動(dòng)效果的實(shí)現(xiàn)思路,需要的朋友可以參考下2022-12-12
Android存儲(chǔ)卡讀寫文件與Application數(shù)據(jù)保存的實(shí)現(xiàn)介紹
這篇文章主要介紹了Android在存儲(chǔ)卡上讀寫文件、Application保存數(shù)據(jù)的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-09-09
詳解Android中Fragment的兩種創(chuàng)建方式
本篇文章主要介紹了Android中Fragment的兩種創(chuàng)建方式,具有一定的參考價(jià)值,有興趣的可以了解一下。2016-12-12
Android TextWatcher三個(gè)回調(diào)以及監(jiān)聽EditText的輸入案例詳解
這篇文章主要介紹了Android TextWatcher三個(gè)回調(diào)以及監(jiān)聽EditText的輸入案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
Android自定義水平進(jìn)度條的圓角進(jìn)度
這篇文章主要為大家詳細(xì)介紹了Android自定義水平進(jìn)度條的圓角進(jìn)度,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
Android學(xué)習(xí)筆記45之gson解析json
JSON即JavaScript Object Natation,是一種輕量級(jí)的數(shù)據(jù)交換格式,采用完全獨(dú)立于語(yǔ)言的文本格式,為Web開發(fā)提供了一種理想的數(shù)據(jù)交換格式。通過(guò)本篇文章給大家介紹Android學(xué)習(xí)筆記45之gson解析json的相關(guān)內(nèi)容,對(duì)android gson解析json相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2015-12-12

