Android控件之SlidingDrawer(滑动式抽屉)
jopen
12年前
一、简介
SlidingDrawer隐藏屏外的内容,并允许用户通过handle以显示隐藏内容。它可以垂直或水平滑动,它有俩个View组成,其一是可以拖动的 handle,其二是隐藏内容的View。它里面的控件必须设置布局,在布局文件中必须指定handle和content。例:
<SlidingDrawer android:id="@+id/slidingDrawer" android:layout_width="fill_parent" android:layout_height="wrap_content" android:handle="@+id/slidingDrawerButton" android:content="@+id/content" android:background="#ffffff"> <Button android:id="@+id/slidingDrawerButton" android:layout_width="fill_parent" android:layout_height="wrap_content"> </Button> <LinearLayout android:id="@+id/content" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ffffff"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content"> </Button> <EditText android:id="@+id/editText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00ff00"> </EditText> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello" android:background="#0000ff" /> </LinearLayout> </SlidingDrawer>
二、重要属性
android:allowSingleTap:指示是否可以通过handle打开或关闭
android:animateOnClick:指示是否当使用者按下手柄打开/关闭时是否该有一个动画。
android:content:隐藏的内容
android:handle:handle(手柄)
三、重要方法
animateClose():关闭时实现动画
close():即时关闭
getContent():获取内容
isMoving():指示SlidingDrawer是否在移动
isOpened():指示SlidingDrawer是否已全部打开
lock():屏蔽触摸事件
setOnDrawerCloseListener(SlidingDrawer.OnDrawerCloseListener onDrawerCloseListener):SlidingDrawer关闭时调用
unlock():解除屏蔽触摸事件
toggle():切换打开和关闭的抽屉SlidingDrawer
四、完整实例
1.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" > <LinearLayout android:id="@+id/linearLayout" android:layout_width="fill_parent" android:layout_height="100px" android:background="#0000ff"> </LinearLayout> <SlidingDrawer android:id="@+id/slidingDrawer" android:layout_width="fill_parent" android:layout_height="wrap_content" android:handle="@+id/slidingDrawerButton" android:content="@+id/content" android:background="#ffffff"> <Button android:id="@+id/slidingDrawerButton" android:layout_width="fill_parent" android:layout_height="wrap_content"> </Button> <LinearLayout android:id="@+id/content" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ffffff"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content"> </Button> <EditText android:id="@+id/editText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00ff00"> </EditText> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello" android:background="#0000ff" /> </LinearLayout> </SlidingDrawer> </LinearLayout>
2.
SlidingDrawer.java package SlidingDrawer.com; import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.widget.LinearLayout; public class SlidingDrawer extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); android.widget.SlidingDrawer mDrawer; final LinearLayout mLinearLayout; mDrawer=(android.widget.SlidingDrawer)findViewById(R.id.slidingDrawer); mDrawer.open(); mLinearLayout=(LinearLayout)findViewById(R.id.linearLayout); mDrawer.setOnDrawerOpenListener(new android.widget.SlidingDrawer.OnDrawerOpenListener() { public void onDrawerOpened() { // TODO Auto-generated method stub LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) mLinearLayout.getLayoutParams(); linearParams.height=100; mLinearLayout.setLayoutParams(linearParams); } }); mDrawer.setOnDrawerCloseListener(new android.widget.SlidingDrawer.OnDrawerCloseListener(){ public void onDrawerClosed() { // TODO Auto-generated method stub LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) mLinearLayout.getLayoutParams(); linearParams.height=400; mLinearLayout.setLayoutParams(linearParams); } }); mDrawer.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub return false; } }); } }