1、GridView是宫格视图,其定义很简单,下面说一下里面的主要属性:
android:horizontalSpacing="5dp"、android:verticalSpacing="5dp": 两列之间的距离、两行之间的距离
android:stretchMode="columnWidth":缩放模式
android:padding="10dp":内边距
android:numColumns="4":定义几列
SlidingDrawer是抽屉类,它的用法很简单,主要包括handle和content,它可以垂直和水平滑动,handle是手动把手,content就是抽屉里的内容;
2、首先看效果图
我写 的只是一个简单的实现效果图,并没有实现更多的事件;
下面代码是实现代码,请看:
第一是自定义handle.xml:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 指定按钮按钮下时的图片 --> <item android:state_pressed="true" android:drawable="@drawable/hook_bg" /> <!-- 指定按钮松开时的图片 --> <item android:state_pressed="false" android:drawable="@drawable/hook" /> </selector>
<?xml version="1.0" encoding="UTF-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_horizontal" android:orientation="horizontal" android:padding="5pt" > <!-- 显示图片 --> <ImageView android:id="@+id/image" android:layout_width="40dp" android:layout_height="50dp" /> <!-- 显示图片下面的文字 --> <TextView android:id="@+id/ItemText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/image" android:layout_centerHorizontal="true" android:text="TextView" /> </RelativeLayout>
布局文件里的组件很简单,只是一个imageView和 TextView组件,
第三是main.xml:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="请点拉下面的钩子" /> <!-- 实现抽屉效果的SlidingDrawer --> <SlidingDrawer android:id="@+id/slidingdrawer" android:layout_width="fill_parent" android:layout_height="fill_parent" android:content="@+id/content" android:handle="@+id/handle" android:orientation="vertical" > <!-- 自定义按钮的图片的handle --> <ImageView android:id="@+id/handle" android:layout_width="80dp" android:layout_height="40dp" android:background="@drawable/handle" /> <!-- 抽屉里的内容content --> <LinearLayout android:id="@+id/content" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/bg" > <!-- 定义一个GridView组件 --> <GridView android:id="@+id/grid" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:horizontalSpacing="5dp" android:numColumns="4" android:padding="10dp" android:stretchMode="columnWidth" android:verticalSpacing="5dp" > </GridView> </LinearLayout> </SlidingDrawer> </LinearLayout>
第四是java代码SlidingDrawerDemoActivity.java :
package cn.csdn.activity; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.widget.GridView; import android.widget.SimpleAdapter; public class SlidingDrawerDemoActivity extends Activity { private int[] images = { R.drawable.dianhua, R.drawable.diqiu, R.drawable.sezi, R.drawable.tupian, R.drawable.wu, R.drawable.xinxi, R.drawable.red, R.drawable.purple, R.drawable.blue, R.drawable.jay0, R.drawable.jay1, R.drawable.jay2, R.drawable.jay3, R.drawable.jay4, R.drawable.jay5 }; private GridView gridView;// 宫格组件 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); gridView = (GridView) findViewById(R.id.grid); // 创建一个List对象,List对象的元素是Map List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>(); for (int i = 0; i < images.length; i++) { Map<String, Object> listItem = new HashMap<String, Object>(); listItem.put("image", images[i]); listItem.put("text", i + 1); listItems.add(listItem); } // 定义简单适配器 SimpleAdapter adapter = new SimpleAdapter(this, listItems, R.layout.cell, new String[] { "image", "text" }, new int[] { R.id.image, R.id.ItemText }); gridView.setAdapter(adapter); } }
http://download.csdn.net/download/rhljiayou/4009766