【Android】九宫格的实现
2015-06-11 13:51
417 查看
第一步,布局文件
第二步,在你的activity 中调用初始化Grid
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".JokeTabHostActivity" android:orientation="vertical" > <GridView android:id="@+id/GridView" android:listSelector="@android:color/transparent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:columnWidth="90dp" android:gravity="center" android:numColumns="auto_fit" android:layout_marginTop="10dp" android:stretchMode="columnWidth"> </GridView> </LinearLayout>
第二步,在你的activity 中调用初始化Grid
GridView gridview = (GridView) findViewById(R.id.GridView); ArrayList<HashMap<String, Object>> meumList = new ArrayList<HashMap<String, Object>>(); for (int i = 1; i < 4; i++) { HashMap<String, Object> map = new HashMap<String, Object>(); if (i == 1) { map.put("ItemImage", R.drawable.a1); map.put("ItemText", "文章精选"); } else if (i == 2) { map.put("ItemImage", R.drawable.a2); map.put("ItemText", "幽默笑话"); } else { map.put("ItemImage", R.drawable.a5); map.put("ItemText", "期待更多"); } meumList.add(map); } SimpleAdapter saItem = new SimpleAdapter(this, meumList, // 数据源 R.layout.item, // xml实现 new String[] { "ItemImage", "ItemText" }, // 对应map的Key new int[] { R.id.ItemImage, R.id.ItemText }); // 对应R的Id // //添加Item到网格中 gridview.setAdapter(saItem); // 添加点击事件 gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @SuppressWarnings("static-access") public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { int index = arg2 + 1;// id是从0开始的,所以需要+1 if (index == 1) { Intent intent = new Intent(); intent.setClass(MainActivity.this, CloseTabHostActivity.class); startActivity(intent); } if (index == 2) { Intent intent = new Intent(); intent.setClass(MainActivity.this, XListViewActivity.class); startActivity(intent); //showInfo("正在努力码代码中。。。"); // finish();//停止当前的Activity,如果不写,则按返回键会跳转回原来的Activity } if (index == 3) { showInfo("期待你的建议,好建议请Q 649175826"); } } });
相关文章推荐
- 【Android】九宫格的实现
- android 调用摄像头拍照
- 电脑通过android中的telnet服务远程访问
- Android 动态设置TextView drawableLeft的方式
- Android Fragment内嵌Fragment页面不刷新数据问题
- android在线更新镜像服务器
- 【Android笔记】Unable to execute dex: Multiple dex files define 解决方法
- android 实现照相功能 照片存放在SID卡中,将照片显示在Image中
- Android Studio个人常用快捷键
- android页面多状态处理
- 【Android】android图片轮播
- 【Android】android图片轮播
- Android发送邮件
- Android反编译工具简介
- android performClick使用
- android service详解
- Android Studio 快捷键
- Unable to resolve target 'android-XX'
- android获取view高度方法
- Android设计模式(十一)--观察者模式