android 的 launcher 有一個抽屜效果,可以有拉出和關閉的效果. 這里主要討論如何實現這種效果.
第一步, 將slidingdraw 控件添加到相關的layout中.代碼如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<SlidingDrawer android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:handle="@+id/handle" android:content="@+id/content">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/handle" android:background="@drawable/handle" android:textSize="20dp"></Button>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:orientation="vertical" android:id="@+id/content" android:layout_h eight="wrap_content">
</LinearLayout>
</SlidingDrawer>
</LinearLayout>
有幾個屬性要注意, 先說slidingdrawer的屬性.
android:handle="@+id/handle" 這個屬性指定的是那一個控件的相應將啟動這個SlidingDraw 這里是一個Button控件
android:content="@+id/content" 這個屬性指定的SlidingDrawer的內容,若是SlidingDraw啟動后, 應該調出那個內容,這里是一個linelayout
除此之外, Button里面設置了一個selector, android:background="@drawable/handle", 這樣在不同的響應中就有不同的背景
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true" android:drawable="@drawable/tray_handle_normal" />
<item android:state_pressed="true" android:drawable="@drawable/tray_handle_pressed" />
<item android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/tray_handle_selected" />
<item android:state_enabled="true" android:drawable="@drawable/tray_handle_normal" />
<item android:state_focused="true" android:drawable="@drawable/tray_handle_selected" />
</selector>