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

没有检索到摘要

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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、实现效果如图所示

1dd9a6807a24f6ea8f619306a4153e59.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值