Android高手进阶教程(二)之----Android Launcher抽屉类SlidingDrawer的使用!

来源:互联网 发布:u盘数据恢复后是乱码 编辑:程序博客网 时间:2024/05/16 14:41
最近在研究Lanucher ,看了源码,发现了SlidingDrawer 这个类,也就是所谓的"抽屉"类。它的用法很简单,要包括handle ,和content .
handle 就是当你点击它的时候,content 要么抽抽屉要么关抽屉。别的不多说了,具体步骤如下.
1.新建Android 工程,命名为SlidingDrawer .
2.准备素材,在这里我的图标是用Launcher2 里面的图标,放在drawable-hdpi 文件夹目录结构如下:
 
3.设置main.xml 布局:代码如下:
view plaincopy to clipboardprint?
 
  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 ,代码如下:
  1. view plaincopy to clipboardprint?  
  2. <?xml version="1.0" encoding="utf-8"?> 
  3. <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
  4.     <item android:state_window_focused="false" android:state_enabled="true" android:drawable="@drawable/handle_normal" /> 
  5.     <item android:state_pressed="true" android:drawable="@drawable/handle_pressed" /> 
  6.     <item android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/handle_focused" /> 
  7.     <item android:state_enabled="true" android:drawable="@drawable/handle_normal" /> 
  8.     <item android:state_focused="true" android:drawable="@drawable/handle_focused" /> 
  9. </selector> 
5.运行之。将会得到如下效果:
 
 
的比较简单呵呵,如果想深入了解,大家看Launcher 源码吧!

本文出自 “Android_Tutor” 博客,请务必保留此出处http://weizhulin.blog.51cto.com/1556324/311461
原创粉丝点击