900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > android 九宫格封装 Android 九宫格的实现方法

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

时间:2019-09-23 03:04:22

相关推荐

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。