Android Launcher抽屜類SlidingDrawer的使用!

來源:互聯網
上載者:User

轉自: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
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:background="#808080"  
  7.     >  
  8. <SlidingDrawer  
  9.     android:id="@+id/slidingdrawer"  
  10.     android:layout_width="fill_parent"  
  11.     android:layout_height="fill_parent"  
  12.     android:orientation="vertical"  
  13.     android:handle="@+id/handle"  
  14.     android:content="@+id/content">  
  15.     <Button  
  16.             android:id="@+id/handle"  
  17.             android:layout_width="88dip"  
  18.             android:layout_height="44dip"  
  19.             android:background="@drawable/handle"  
  20.         />  
  21.     <LinearLayout  
  22.         android:id="@+id/content"  
  23.         android:layout_width="fill_parent"  
  24.         android:layout_height="fill_parent"  
  25.         android:background="#00ff00">  
  26.         <Button  
  27.             android:id="@+id/button"  
  28.             android:layout_width="wrap_content"  
  29.             android:layout_height="wrap_content"  
  30.             android:text="Button"  
  31.         />  
  32.         <EditText  
  33.             android:id="@+id/editText"  
  34.             android:layout_width="fill_parent"  
  35.             android:layout_height="wrap_content"  
  36.         />  
  37.     </LinearLayout>  
  38. </SlidingDrawer>  
  39. </LinearLayout>  

 

4.設定handle表徵圖的樣式,在drawable裡添加handle.xml,代碼如下:

 

[java]
view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item android:state_window_focused="false" android:state_enabled="true" android:drawable="@drawable/handle_normal" />  
  4.     <item android:state_pressed="true" android:drawable="@drawable/handle_pressed" />  
  5.     <item android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/handle_focused" />  
  6.     <item android:state_enabled="true" android:drawable="@drawable/handle_normal" />  
  7.     <item android:state_focused="true" android:drawable="@drawable/handle_focused" />  
  8. </selector>  

 

5.運行之。將會得到如下效果:

 

 

 

的比較簡單呵呵,如果想深入瞭解,大家看Launcher源碼吧!

最近在研究Lanucher,看了源碼,發現了SlidingDrawer這個類,也就是所謂的"抽屜"類。它的用法很簡單,要包括handle,和content.

 

handle就是當你點擊它的時候,content要麼抽抽屜要麼關抽屜。別的不多說了,具體步驟如下.

 

1.建立Android工程,命名為SlidingDrawer.

 

2.準備素材,在這裡我的表徵圖是用Launcher2裡面的表徵圖,放在drawable-hdpi檔案夾目錄結構如下:

 

 

 

3.設定main.xml布局:代碼如下:

 

[java]
view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:background="#808080"  
  7.     >  
  8. <SlidingDrawer  
  9.     android:id="@+id/slidingdrawer"  
  10.     android:layout_width="fill_parent"  
  11.     android:layout_height="fill_parent"  
  12.     android:orientation="vertical"  
  13.     android:handle="@+id/handle"  
  14.     android:content="@+id/content">  
  15.     <Button  
  16.             android:id="@+id/handle"  
  17.             android:layout_width="88dip"  
  18.             android:layout_height="44dip"  
  19.             android:background="@drawable/handle"  
  20.         />  
  21.     <LinearLayout  
  22.         android:id="@+id/content"  
  23.         android:layout_width="fill_parent"  
  24.         android:layout_height="fill_parent"  
  25.         android:background="#00ff00">  
  26.         <Button  
  27.             android:id="@+id/button"  
  28.             android:layout_width="wrap_content"  
  29.             android:layout_height="wrap_content"  
  30.             android:text="Button"  
  31.         />  
  32.         <EditText  
  33.             android:id="@+id/editText"  
  34.             android:layout_width="fill_parent"  
  35.             android:layout_height="wrap_content"  
  36.         />  
  37.     </LinearLayout>  
  38. </SlidingDrawer>  
  39. </LinearLayout>  

 

4.設定handle表徵圖的樣式,在drawable裡添加handle.xml,代碼如下:

 

[java]
view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item android:state_window_focused="false" android:state_enabled="true" android:drawable="@drawable/handle_normal" />  
  4.     <item android:state_pressed="true" android:drawable="@drawable/handle_pressed" />  
  5.     <item android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/handle_focused" />  
  6.     <item android:state_enabled="true" android:drawable="@drawable/handle_normal" />  
  7.     <item android:state_focused="true" android:drawable="@drawable/handle_focused" />  
  8. </selector>  

 

5.運行之。將會得到如下效果:

 

 

 

的比較簡單呵呵,如果想深入瞭解,大家看Launcher源碼吧!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.