SlidingDrawer类使用的时候主要包括两点:
1:handle:单击的按钮
下面给出一个实例
2.GridView中的布局代码
3.BaseAdapter的子类实现(为GridView提供数据)
4.主程序的实现
1:handle:单击的按钮
2:content:抽屉中的内容,单击按钮时,抽屉的内容隐藏或显示
下面这段xml布局是摘自Google SDK帮助文档:
<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" /> <GridView android:id="@id/content" android:layout_width="match_parent" android:layout_height="match_parent" /> </SlidingDrawer>
下面给出一个实例
首先实例的实现截图如下:
实现滑动效果
给出如下实现代码:
1.总体布局文件的代码
<?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" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:textSize="16sp" /> <SlidingDrawer android:id="@+id/drawer1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:handle="@+id/layout1" android:content="@+id/myContent1" android:orientation="horizontal" > <LinearLayout android:id="@id/layout1" android:layout_width="35px" android:layout_height="fill_parent" android:background="@drawable/black" android:gravity="center" > <ImageView android:id="@+id/myImage1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/open" /> </LinearLayout> <GridView android:id="@id/myContent1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numColumns="2" android:background="@drawable/black" android:gravity="center" /> </SlidingDrawer> </RelativeLayout>
2.GridView中的布局代码
<?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" > <ImageView android:id="@+id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <TextView android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="20sp" android:gravity="center" android:textColor="@drawable/white" /> </LinearLayout>
3.BaseAdapter的子类实现(为GridView提供数据)
/* 自定义Adapter,继承BaseAdapter */ public class MyGridViewAdapter extends BaseAdapter { private Context _con; private String[] _items; private int[] _icons; /* 构造符 */ public MyGridViewAdapter(Context con,String[] items,int[] icons) { _con=con; _items=items; _icons=icons; } @Override public int getCount() { return _items.length; } @Override public Object getItem(int arg0) { return _items[arg0]; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater factory = LayoutInflater.from(_con); /* 使用grid.xml为每几个item的Layout */ View v = (View) factory.inflate(R.layout.grid, null); /* 取得View */ ImageView iv = (ImageView) v.findViewById(R.id.icon); TextView tv = (TextView) v.findViewById(R.id.text); /* 设定显示的Image与文? */ iv.setImageResource(_icons[position]); tv.setText(_items[position]); return v; } }
4.主程序的实现
public class EX04_27 extends Activity { private GridView gv; private SlidingDrawer sd; private ImageView im; private int[] icons={R.drawable.alarm,R.drawable.calendar, R.drawable.camera,R.drawable.clock, R.drawable.music,R.drawable.tv}; private String[] items={"Alarm","Calendar","Camera","Clock","Music","TV"}; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* 加载main.xml Layout */ setContentView(R.layout.main); /* 初始化对象 */ gv = (GridView)findViewById(R.id.myContent1); sd = (SlidingDrawer)findViewById(R.id.drawer1); im=(ImageView)findViewById(R.id.myImage1); /* 使用告定义的MyGridViewAdapter设置GridView里面的item内容 */ MyGridViewAdapter adapter=new MyGridViewAdapter(this,items,icons); gv.setAdapter(adapter); /* 设定SlidingDrawer被打开的事件处理 */ sd.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() { @Override public void onDrawerOpened() { im.setImageResource(R.drawable.close); } }); /* 设置SlidingDrawer被关闭的事件处理 */ sd.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() { @Override public void onDrawerClosed() { im.setImageResource(R.drawable.open); } }); } }
作者:DLUTBruceZhang 发表于2013-2-3 0:11:14 原文链接
阅读:105 评论:0 查看评论