SlidingDrawer 和 GridView

本文介绍如何使用 Android 的 SlidingDrawer 类实现抽屉效果。通过设置 handle 和 content 属性,结合 GridView 和自定义适配器,展示了创建交互式滑动抽屉的基本步骤。

SlidingDrawer这个类,也就是所谓的"抽屉"类。它的用法很简单,要包括handle,和content.


handle就是当你点击它的时候,content要么抽抽屉要么关抽屉。别的不多说了,具体步骤如下.

效果如下所示:

点击"抽屉"前:



点击"抽屉"后:


工程目录如下:



mail.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:background="#808080" > <SlidingDrawer android:id="@+id/drawer" android:layout_width="match_parent" android:layout_height="match_parent" android:handle="@+id/handle" android:content="@+id/content"> <ImageView android:id="@id/handle" android:layout_width="88dip" android:layout_height="44dip" android:src="@drawable/android1" /> <GridView android:id="@id/content" android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="auto_fit" android:columnWidth="90px" android:background="#999099ff" /> </SlidingDrawer> </LinearLayout>

image_view.xml文件如下:


<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:paddingBottom="4dip" android:layout_width="fill_parent"> <ImageView android:layout_height="wrap_content" android:id="@+id/ItemImage" android:layout_width="wrap_content" android:layout_centerHorizontal="true"> </ImageView> <TextView android:layout_width="wrap_content" android:layout_below="@+id/ItemImage" android:layout_height="wrap_content" android:text="TextView01" android:layout_centerHorizontal="true" android:id="@+id/ItemText"> </TextView> </RelativeLayout>

activity代码如下:


package mzz.drawer; import java.util.ArrayList; import java.util.HashMap; import android.app.Activity; import android.os.Bundle; import android.widget.GridView; import android.widget.SimpleAdapter; public class SlidingDrawerActivity extends Activity { /** Called when the activity is first created. */ private GridView content = null ; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); content = (GridView)findViewById(R.id.content); ArrayList<HashMap<String,Object>> array = new ArrayList<HashMap<String,Object>>(); for(int i = 1 ; i < 14 ; i ++) { HashMap<String , Object> map = new HashMap<String, Object>(); map.put("ItemImage", R.drawable.icon); map.put("ItemText", "No." + i); array.add(map); } SimpleAdapter adapter = new SimpleAdapter(this, array, R.layout.image_view, new String[]{"ItemImage" , "ItemText"}, new int[]{R.id.ItemImage,R.id.ItemText}); content.setAdapter(adapter); } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值