运行结果
1.图
2.动图
九个图标是设置的,你也可以设置4 * 4 = 16都可以。
3.分享个gif动图的.exe
代码activity_main.xml
item.xml
mainactivity.java
package edu.hrbust.syoneninelayout; import android.annotation.suppresslint; import android.app.activity; import android.os.bundle; import android.view.view; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.gridview; import android.widget.simpleadapter; import android.widget.toast; import java.util.arraylist; import java.util.hashmap; public class mainactivity extends activity { // 调用活动 @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // 关联activity_main.xml 和 控件gridview setcontentview(r.layout.activity_main); gridview gridview = (gridview) findviewbyid(r.id.gridview); // 构建arraylist是hashmap格式,hashmap格式是arraylist > meumlist = new arraylist >(); // 向arraylist填入数据 图片 i i设置为1~9,形成九宫格 for(int i = 1; i < 10; i ){ hashmap map = new hashmap (); //r.mipmap.ic_luminggai是关联的图片 我的图片在res的mipmap下面 map.put("itemimage",r.mipmap.ic_luminggai); map.put("itemtext","" i); meumlist.add(map); } // 构建数据适配器 simpleadapter saitem = new simpleadapter(this, meumlist, r.layout.item, new string[]{"itemimage","itemtext"}, new int[]{r.id.itemimage,r.id.itemtext}); // 应用适配器 gridview.setadapter(saitem); gridview.setonitemclicklistener( new onitemclicklistener() { @suppresslint("wrongconstant") public void onitemclick(adapterview arg0, view arg1, int arg2, long arg3) { int index = arg2 1; toast.maketext(getapplicationcontext(),"点击了选项:" index,0).show(); } } ); } }