(转)Android控件之——SlidingDrawer的使用及重要方法

来源:互联网 发布:结婚证合成软件 编辑:程序博客网 时间:2024/06/03 09:19

原地址:http://blog.csdn.net/moreevan/article/details/6741083


我们来看一下官方文档中对这个控件 的定义:

SlidingDrawer hides content out of the screen and allows the user to drag a handle to bring the content on screen. SlidingDrawer can be used vertically or horizontally. A special widget composed of two children views: the handle, that the users drags, and the content, attached to the handle and dragged with it. SlidingDrawer should be used as an overlay inside layouts. This means SlidingDrawer should only be used inside of a FrameLayout or a RelativeLayout for instance. The size of the SlidingDrawer defines how much space the content will occupy once slid out so SlidingDrawer should usually use match_parent for both its dimensions. Inside an XML layout, SlidingDrawer must define the id of the handle and of the content:

其实android1.5的应用程序列表就应用了这个控件。

我们来看一下它特有的xml属性

XML AttributesAttribute NameRelated MethodDescriptionandroid:allowSingleTap Indicates whether the drawer can be opened/closed by a single tap on the handle. android:animateOnClick Indicates whether the drawer should be opened/closed with an animation when the user clicks the handle. android:bottomOffset Extra offset for the handle at the bottom of the SlidingDrawer. android:content Identifier for the child that represents the drawer's content. android:handle Identifier for the child that represents the drawer's handle. android:orientation Orientation of the SlidingDrawer. android:topOffset Extra offset for the handle at the top of the SlidingDrawer. 这里都很清楚了,英文也很简单,我就不多说 了,直接上demo。

先来看一下最终的效果截个图:


两张图分别对应SlidingDrawer关闭时和打开时的视图


看一下layout的定义

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <SlidingDrawer xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/drawer" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent" android:handle="@+id/handle"  
  5.     android:content="@+id/content">  
  6.   
  7.     <ImageView android:id="@id/handle" android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content" android:src="@drawable/up"></ImageView>  
  9.     <ListView android:id="@id/content" android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent" android:background="#ff00ff"></ListView>  
  11.   
  12. </SlidingDrawer>  

这里在SlidingDrawer元素里必须指定它的handle和content属性

再看一下Activity的实现:

[java] view plain copy
  1. package kevin.droid;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.AdapterView;  
  7. import android.widget.AdapterView.OnItemClickListener;  
  8. import android.widget.ArrayAdapter;  
  9. import android.widget.ImageView;  
  10. import android.widget.ListView;  
  11. import android.widget.SlidingDrawer;  
  12. import android.widget.SlidingDrawer.OnDrawerCloseListener;  
  13. import android.widget.SlidingDrawer.OnDrawerOpenListener;  
  14. import android.widget.Toast;  
  15.   
  16. public class SlidingDemo extends Activity implements OnItemClickListener,  
  17.         OnDrawerOpenListener, OnDrawerCloseListener {  
  18.     private SlidingDrawer drawer;  
  19.     private ImageView handle;  
  20.     private ListView content;  
  21.   
  22.     @Override  
  23.     public void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.sliding);  
  26.   
  27.         drawer = (SlidingDrawer) this.findViewById(R.id.drawer);  
  28.         handle = (ImageView) this.findViewById(R.id.handle);  
  29.         content = (ListView) this.findViewById(R.id.content);  
  30.         content.setAdapter(new ArrayAdapter<String>(this,  
  31.                 android.R.layout.simple_list_item_1, new String[] { "one",  
  32.                         "two""three" }));  
  33.         content.setOnItemClickListener(this);  
  34.   
  35.         // 设置SlidingDrawer打开或者关闭时的监听器  
  36.         drawer.setOnDrawerOpenListener(this);  
  37.         drawer.setOnDrawerCloseListener(this);  
  38.     }  
  39.   
  40.     @Override  
  41.     public void onItemClick(AdapterView<?> parent, View view, int position,  
  42.             long id) {  
  43.         Toast.makeText(this"you clicked position " + (position + 1),  
  44.                 Toast.LENGTH_SHORT).show();  
  45.     }  
  46.   
  47.     // SlidingDrawer关闭时回调  
  48.     @Override  
  49.     public void onDrawerClosed() {  
  50.         handle.setImageResource(R.drawable.up);  
  51.     }  
  52.   
  53.     // SlidingDrawer打开时回调  
  54.     @Override  
  55.     public void onDrawerOpened() {  
  56.         handle.setImageResource(R.drawable.down);  
  57.     }  
  58.   
  59. }  


它最重要的两个方法就是在代码中提到的

voidsetOnDrawerCloseListener(SlidingDrawer.OnDrawerCloseListener onDrawerCloseListener)
Sets the listener that receives a notification when the drawer becomes close.
voidsetOnDrawerOpenListener(SlidingDrawer.OnDrawerOpenListener onDrawerOpenListener)
Sets the listener that receives a notification when the drawer becomes open.
这是一个非常简单的demo,供刚开始学习Android的童鞋参考,已经有经验的童鞋请轻喷。。。
0 0
原创粉丝点击