Android 九宫格的实现方法
程序员文章站
2023-12-10 21:48:04
1、xml代码:复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:background="@drawable/yellow"
>
<imageview android:id="@+id/imageview01"
android:layout_width="100sp"
android:layout_height="100sp"
android:layout_gravity="center_vertical"
android:background="@drawable/a"></imageview>
<gridview
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"
>
</gridview>
</linearlayout>
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<hashmap<string, object>> lstimageitem = new arraylist<hashmap<string, object>>();
/**
* 为每个格子添加内容
*/
for (int i = 1; i < 10; i++) {
hashmap<string, object> map = new hashmap<string, object>();// 建立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<string, object> item = (hashmap<string, object>) 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、实现效果如图所示
1、xml代码:
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:background="@drawable/yellow"
>
<imageview android:id="@+id/imageview01"
android:layout_width="100sp"
android:layout_height="100sp"
android:layout_gravity="center_vertical"
android:background="@drawable/a"></imageview>
<gridview
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"
>
</gridview>
</linearlayout>
其中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<hashmap<string, object>> lstimageitem = new arraylist<hashmap<string, object>>();
/**
* 为每个格子添加内容
*/
for (int i = 1; i < 10; i++) {
hashmap<string, object> map = new hashmap<string, object>();// 建立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<string, object> item = (hashmap<string, object>) 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中使用Toast.cancel()方法优化toast内容显示的解决方法
-
解析Android中使用自定义字体的实现方法
-
Android日期时间格式国际化的实现代码
-
解析android中隐藏与显示软键盘及不自动弹出键盘的实现方法
-
基于Android开发支持表情的实现详解
-
解析Android中实现滑动翻页之ViewFlipper的使用详解
-
解析Android中如何做到Service被关闭后又自动启动的实现方法
-
iOS应用开发中使UITextField实现placeholder属性的方法
-
解析Android获取系统cpu信息,内存,版本,电量等信息的方法详解