zoukankan      html  css  js  c++  java
  • 【Android】九宫格实现

    第一步,布局文件


    <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"); } } });



    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    夺命雷公狗---javascript NO:11 事件对象1
    夺命雷公狗---javascript NO:10 解决事件监听兼容性问题和移除事件
    夺命雷公狗---javascript NO:09 事件绑定的种类1
    夺命雷公狗---javascript NO:08 常用的事件
    夺命雷公狗---javascript NO:07 事件编程介绍
    夺命雷公狗---javascript NO:06 数组定义和遍历
    夺命雷公狗---javascript NO:05 js函数中的作用域
    夺命雷公狗---javascript NO:04 js中的函数
    夺命雷公狗---javascript NO:03 流程结构
    夺命雷公狗---javascript NO:02 数据类型和运算符
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4841848.html
Copyright © 2011-2022 走看看