轉自:http://blog.csdn.net/android_tutor/article/details/5486804
最近在研究Lanucher,看了源碼,發現了SlidingDrawer這個類,也就是所謂的"抽屜"類。它的用法很簡單,要包括handle,和content.
handle就是當你點擊它的時候,content要麼抽抽屜要麼關抽屜。別的不多說了,具體步驟如下.
1.建立Android工程,命名為SlidingDrawer.
2.準備素材,在這裡我的表徵圖是用Launcher2裡面的表徵圖,放在drawable-hdpi檔案夾目錄結構如下:
3.設定main.xml布局:代碼如下:
[java]
view plaincopy
- <?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"
- android:background="#808080"
- >
- <SlidingDrawer
- android:id="@+id/slidingdrawer"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- android:handle="@+id/handle"
- android:content="@+id/content">
- <Button
- android:id="@+id/handle"
- android:layout_width="88dip"
- android:layout_height="44dip"
- android:background="@drawable/handle"
- />
- <LinearLayout
- android:id="@+id/content"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#00ff00">
- <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>
4.設定handle表徵圖的樣式,在drawable裡添加handle.xml,代碼如下:
[java]
view plaincopy
- <?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/handle_normal" />
- <item android:state_pressed="true" android:drawable="@drawable/handle_pressed" />
- <item android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/handle_focused" />
- <item android:state_enabled="true" android:drawable="@drawable/handle_normal" />
- <item android:state_focused="true" android:drawable="@drawable/handle_focused" />
- </selector>
5.運行之。將會得到如下效果:
的比較簡單呵呵,如果想深入瞭解,大家看Launcher源碼吧!
最近在研究Lanucher,看了源碼,發現了SlidingDrawer這個類,也就是所謂的"抽屜"類。它的用法很簡單,要包括handle,和content.
handle就是當你點擊它的時候,content要麼抽抽屜要麼關抽屜。別的不多說了,具體步驟如下.
1.建立Android工程,命名為SlidingDrawer.
2.準備素材,在這裡我的表徵圖是用Launcher2裡面的表徵圖,放在drawable-hdpi檔案夾目錄結構如下:
3.設定main.xml布局:代碼如下:
[java]
view plaincopy
- <?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"
- android:background="#808080"
- >
- <SlidingDrawer
- android:id="@+id/slidingdrawer"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- android:handle="@+id/handle"
- android:content="@+id/content">
- <Button
- android:id="@+id/handle"
- android:layout_width="88dip"
- android:layout_height="44dip"
- android:background="@drawable/handle"
- />
- <LinearLayout
- android:id="@+id/content"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#00ff00">
- <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>
4.設定handle表徵圖的樣式,在drawable裡添加handle.xml,代碼如下:
[java]
view plaincopy
- <?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/handle_normal" />
- <item android:state_pressed="true" android:drawable="@drawable/handle_pressed" />
- <item android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/handle_focused" />
- <item android:state_enabled="true" android:drawable="@drawable/handle_normal" />
- <item android:state_focused="true" android:drawable="@drawable/handle_focused" />
- </selector>
5.運行之。將會得到如下效果:
的比較簡單呵呵,如果想深入瞭解,大家看Launcher源碼吧!