Android 九宫格的实现方法
时间:2022-08-19 01:07:09
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,九宫格
0
投稿
猜你喜欢
Android开发之资源文件用法实例总结
2023-05-13 06:02:39
区块链java代码实现
2023-02-04 04:00:24
C#使用XML序列化操作菜单的方法
2022-08-03 08:54:34
java实现双色球机选号码生成器
2022-10-05 07:41:33
详解java爬虫jsoup解析多空格class数据
2021-12-30 14:47:41
最新IntelliJ IDEA2017.3 激活方式
2023-08-12 00:47:24
Android自定义View实现带数字的进度条实例代码
2021-05-27 01:31:49
Android下2d物理引擎Box2d用法简单实例
2023-09-11 03:18:02
Java编程计算兔子生兔子的问题
2023-08-01 15:24:37
详解Java图形化编程中的鼠标事件设计
2022-07-11 08:11:31
Java并发编程之JUC并发核心AQS同步队列原理剖析
2023-01-15 15:14:37
Android 各版本兼容性适配详解
2021-08-23 09:20:30
springboot @WebFilter注解过滤器的实现
2023-07-06 11:14:54
IDEA 2020 本土化,真的是全中文了(真香)
2023-11-25 08:02:58
C#调用C++DLL传递结构体数组的终极解决方案
2022-05-31 09:54:30
利用Matlab复刻羊了个羊小游戏
2021-10-10 17:13:05
UE4 Unlua 调用异步蓝图节点AIMoveTo函数示例详解
2022-04-12 05:35:41
Java 通过反射给实体类赋值操作
2023-11-26 10:11:41
SpringBoot整合Mybatis简单实现增删改查
2023-07-05 15:33:44
flutter中build.gradle仓库的配置(解决外网下载速度过慢失败的问题)
2023-06-16 02:07:20