SlidingDrawer是自SDK 1.5才新加入的,實現Launcher的抽屜效果。SlidingDrawer配置上採用了水平展開或垂直展開兩種(android:orientation)方式,在XML裡必須指定其使用的android:handle與android:content,前者委託要展開的圖片(Layout配置),後者則是要展開的Layout
Content。
收縮時的效果 展開時的效果
<?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:background="@drawable/i1" android:orientation="vertical" > <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" > <Button android:id="@+id/handle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/ic_dialog_dialer" /> <LinearLayout android:id="@+id/content" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/default_bg" > <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <EditText android:id="@+id/editText" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> </SlidingDrawer></LinearLayout>
一、簡介
SlidingDrawer隱藏屏外的內容,並允許使用者通過handle以顯示隱藏內容。它可以垂直或水平滑動,它有倆個View組成,其一是可以拖動的handle,其二是隱藏內容的View.它裡面的控制項必須設定布局,在布局檔案中必須指定handle和content.
例如下面
<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" > <ImageButton android:id="@id/handle" android:layout_width="50dip" android:layout_height="44dip" android:src="@drawable/up" /> <LinearLayout android:id="@id/content" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff" > <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical|center_horizontal" android:text="這是一個滑動式抽屜的樣本" android:textColor="#000000" android:textSize="18px" android:textStyle="bold" > </TextView> </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.布局檔案slidingdrawer.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:background="@drawable/default_bg" android:orientation="vertical" > <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" > <ImageButton android:id="@id/handle" android:layout_width="50dip" android:layout_height="44dip" android:src="@drawable/up" /> <LinearLayout android:id="@id/content" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff" > <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical|center_horizontal" android:text="這是一個滑動式抽屜的樣本" android:textColor="#000000" android:textSize="18px" android:textStyle="bold" > </TextView> </LinearLayout> </SlidingDrawer></LinearLayout>
2.Java代碼:
package com.way;import android.app.Activity;import android.os.Bundle;import android.widget.ImageButton;import android.widget.SlidingDrawer;import android.widget.TextView;public class SlidingDrawerDemo extends Activity {private SlidingDrawer mDrawer;private ImageButton imbg;private Boolean flag = false;private TextView tv;/* * (non-Javadoc) * * @see android.app.Activity#onCreate(android.os.Bundle) */@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.sildingdrawer);imbg = (ImageButton) findViewById(R.id.handle);mDrawer = (SlidingDrawer) findViewById(R.id.slidingdrawer);tv = (TextView) findViewById(R.id.tv);mDrawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {@Overridepublic void onDrawerOpened() {flag = true;imbg.setImageResource(R.drawable.down);}});mDrawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {@Overridepublic void onDrawerClosed() {flag = false;imbg.setImageResource(R.drawable.up);}});mDrawer.setOnDrawerScrollListener(new SlidingDrawer.OnDrawerScrollListener() {@Overridepublic void onScrollEnded() {tv.setText("結束拖動");}@Overridepublic void onScrollStarted() {tv.setText("開始拖動");}});}}