步步为营_Android开发课[22]_用户界面之GridView(网格视图)

原创 2015年04月12日 12:31:09

Focus on technology, enjoy life!—— QQ:804212028
浏览链接:http://blog.csdn.net/y18334702058/article/details/44624305


  • 主题:用户界面之GridView(网格视图)
    -九宫格布局就是用它来实现的。

GridView实现九宫格(实例):
activity_main.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"
    >

    <GridView 
        android:id="@+id/gridview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:columnWidth="90dp"
        android:gravity="center"
        android:padding="30dp"
        android:numColumns="3"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        />

</LinearLayout>

给gridview中的item自定义布局:
gridview_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView 
        android:id="@+id/image"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_centerHorizontal="true"/>
    <TextView 
        android:id="@+id/str"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_centerHorizontal="true"/>
</LinearLayout>

MainActivity.java:

import java.util.ArrayList;
import java.util.HashMap;
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;

public class MainActivity extends Activity{

    private int[] image = {R.drawable.myimage,R.drawable.myimage,R.drawable.myimage,R.drawable.myimage
            ,R.drawable.myimage,R.drawable.myimage,R.drawable.myimage,R.drawable.myimage};
    private String[] str = {"宫式布局1", "宫式布局2", "宫式布局3", "宫式布局4", "宫式布局5",
            "宫式布局6", "宫式布局7", "宫式布局8" };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        GridView gridView = (GridView)findViewById(R.id.gridview);

        ArrayList<HashMap<String,Object>> item = new ArrayList<HashMap<String,Object>>();
        for(int i=0; i<image.length; i++){
            HashMap<String,Object> map = new HashMap<String,Object>();
            map.put("image", image[i]);
            map.put("str", str[i]);
            item.add(map);
        }

        SimpleAdapter simpleAdapter = new SimpleAdapter(this, item, R.layout.gridview_item, new String[] { "image", "str" }, new int[] {
                R.id.image, R.id.str });
        gridView.setAdapter(simpleAdapter);
        gridView.setOnItemClickListener(new ItemClickListener());

    }      

    class ItemClickListener implements OnItemClickListener{

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            //这里写监听事件
        }

    }
}

运行结果:

这里写图片描述

Focus on technology, enjoy life!—— QQ:804212028
浏览链接:http://blog.csdn.net/y18334702058/article/details/44624305

相关文章推荐

步步为营_Android开发课[35]_用户界面之WebView(网络视图)

Focus on technology, enjoy life!—— QQ:804212028 浏览链接:http://blog.csdn.net/y18334702058/article/det...

步步为营_Android开发课[25]_用户界面之ScrollView(滚动视图)

Focus on technology, enjoy life!—— QQ:804212028 浏览链接:http://blog.csdn.net/y18334702058/article/det...

步步为营_Android开发课[15]_用户界面之TextView(文本视图)

Focus on technology, enjoy life!—— QQ:804212028 浏览链接:http://blog.csdn.net/y18334702058/article/det...

步步为营_Android开发课[30]_用户界面之ViewFlipper(视图切换)

Focus on technology, enjoy life!—— QQ:804212028 浏览链接:http://blog.csdn.net/y18334702058/article/det...

步步为营_Android开发课[19]_用户界面之ListView(列表视图)

Focus on technology, enjoy life!—— QQ:804212028 浏览链接:http://blog.csdn.net/y18334702058/article/det...

步步为营_Android开发课[16]_用户界面之EditText(编辑视图)

Focus on technology, enjoy life!—— QQ:804212028 浏览链接:http://blog.csdn.net/y18334702058/article/det...

步步为营_Android开发课[13]_用户界面之view(视图)

Focus on technology, enjoy life!—— QQ:804212028 浏览链接:http://blog.csdn.net/y18334702058/article/det...

Android开发——GridView(九宫图)网格视图

我们先列出GridView的一些属性,这里我就借一下别的博主的了谢谢浅秋博主  1.Android:numColumns  //GridView的列数 2.android:columnWidt...

步步为营_Android开发课[21]_用户界面之Notification(通知栏)

Focus on technology, enjoy life!—— QQ:804212028 浏览链接:http://blog.csdn.net/y18334702058/article/det...

步步为营_Android开发课[26]_用户界面之PopupWindow(弹出窗口)

Focus on technology, enjoy life!—— QQ:804212028 浏览链接:http://blog.csdn.net/y18334702058/article/det...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:步步为营_Android开发课[22]_用户界面之GridView(网格视图)
举报原因:
原因补充:

(最多只允许输入30个字)