日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android 九宫格封装,Android 九宫格的实现方法

發布時間:2025/3/15 Android 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 九宫格封装,Android 九宫格的实现方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、xml代碼:

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1.0"

android:background="@drawable/yellow"

>

android:layout_width="100sp"

android:layout_height="100sp"

android:layout_gravity="center_vertical"

android:background="@drawable/a">

android:id="@+id/gridview"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:numcolumns="3"

android:verticalspacing="30dip"

android:horizontalspacing="10dip"

android:columnwidth="90dip"

android:stretchmode="columnwidth"

android:gravity="center"

android:listselector="@drawable/c"

>

其中android:numcolumns="3"? 代表九宮格的列數? auto_fit時為自動

2、實現代碼

public class mainactivity extends activity {

/** called when the activity is first created. */

@override

protected void oncreate(bundle savedinstancestate) {

// todo auto-generated method stub

super.oncreate(savedinstancestate);

// 設置屏幕沒有標題

this.requestwindowfeature(window.feature_no_title);

// 去掉標題欄

this.getwindow().setflags(windowmanager.layoutparams.flag_fullscreen,

windowmanager.layoutparams.flag_fullscreen);

setcontentview(r.layout.activity_main);

gridview gridview = (gridview) findviewbyid(r.id.gridview);

// 創建一個數組列表對象

arraylist> lstimageitem = new arraylist>();

/**

* 為每個格子添加內容

*/

for (int i = 1; i < 10; i++) {

hashmap map = new hashmap();// 建立hashmap對象

if (i == 1) {

map.put("itemimage", r.drawable.g11);

map.put("itemtext", getresources()

.getstring(r.string.gridview1));

}

if (i == 2) {

map.put("itemimage", r.drawable.g12);

map.put("itemtext", getresources()

.getstring(r.string.gridview2));

}

if (i == 3) {

map.put("itemimage", r.drawable.g13);

map.put("itemtext", getresources()

.getstring(r.string.gridview3));

}

if (i == 4) {

map.put("itemimage", r.drawable.g14);

map.put("itemtext", getresources()

.getstring(r.string.gridview4));

}

if (i == 5) {

map.put("itemimage", r.drawable.g15);

map.put("itemtext", getresources()

.getstring(r.string.gridview5));

}

if (i == 6) {

map.put("itemimage", r.drawable.g16);

map.put("itemtext", getresources()

.getstring(r.string.gridview6));

}

if (i == 7) {

map.put("itemimage", r.drawable.g17);

map.put("itemtext", getresources()

.getstring(r.string.gridview7));

}

if (i == 8) {

map.put("itemimage", r.drawable.g18);

map.put("itemtext", getresources()

.getstring(r.string.gridview8));

}

if (i == 9) {

map.put("itemimage", r.drawable.g19);

map.put("itemtext", getresources()

.getstring(r.string.gridview9));

}

lstimageitem.add(map);

}

/**

* 為gridview建立simpleadapter適配器

*/

// simpleadapter()中的五個參數分別是:第一個context,第二個數據資源,第三個每一個子項的布局文件,第四個每一個子項中的key數組

// 第五個每一個子項中的value數組

simpleadapter saimageitems = new simpleadapter(this, lstimageitem,

r.layout.grid_item, new string[] { "itemimage", "itemtext" },

new int[] { r.id.itemimage, r.id.itemtext });

gridview.setadapter(saimageitems);// 添加適配器

gridview.setonitemclicklistener(new itemclicklistener());// 為每一個子項設置監聽

}

class itemclicklistener implements onitemclicklistener {

@suppresswarnings("unchecked")

public void onitemclick(adapterview> arg0,// the adapterview where the

// click happened

view arg1,// the view within the adapterview that was clicked

int arg2,// the position of the view in the adapter

long arg3// the row id of the item that was clicked

) {

hashmap item = (hashmap) arg0

.getitematposition(arg2);

if (item.get("itemtext").equals(

getresources().getstring(r.string.gridview1))) {

toast.maketext(mainactivity.this, r.string.gridview1,

toast.length_long).show();

}

if (item.get("itemtext").equals(

getresources().getstring(r.string.gridview2))) {

toast.maketext(mainactivity.this, r.string.gridview2,

toast.length_long).show();

}

if (item.get("itemtext").equals(

getresources().getstring(r.string.gridview3))) {

toast.maketext(mainactivity.this, r.string.gridview3,

toast.length_long).show();

}

if (item.get("itemtext").equals(

getresources().getstring(r.string.gridview4))) {

toast.maketext(mainactivity.this, r.string.gridview4,

toast.length_long).show();

}

if (item.get("itemtext").equals(

getresources().getstring(r.string.gridview5))) {

toast.maketext(mainactivity.this, r.string.gridview5,

toast.length_long).show();

}

if (item.get("itemtext").equals(

getresources().getstring(r.string.gridview6))) {

toast.maketext(mainactivity.this, r.string.gridview6,

toast.length_long).show();

}

if (item.get("itemtext").equals(

getresources().getstring(r.string.gridview7))) {

toast.maketext(mainactivity.this, r.string.gridview7,

toast.length_long).show();

}

if (item.get("itemtext").equals(

getresources().getstring(r.string.gridview8))) {

toast.maketext(mainactivity.this, r.string.gridview8,

toast.length_long).show();

}

if (item.get("itemtext").equals(

getresources().getstring(r.string.gridview9))) {

toast.maketext(mainactivity.this, r.string.gridview9,

toast.length_long).show();

}

}

}

}

3、實現效果如圖所示

總結

以上是生活随笔為你收集整理的android 九宫格封装,Android 九宫格的实现方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。