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

最近在研究Lanucher,看了源码,发现了SlidingDrawer这个类,也就是所谓的"抽屉"类。它的用法很简单,要包括handle,和content.

handle就是当你点击它的时候,content要么抽抽屉要么关抽屉。别的不多说了,具体步骤如下.

1.新建Android工程,命名为SlidingDrawer.

2.准备素材,在这里我的图标是用Launcher2里面的图标,放在drawable-hdpi文件夹目录结构如下:

3.设置main.xml布局:代码如下:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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. <?xmlversion="1.0"encoding="utf-8"?>
  2. <selectorxmlns:android="http://schemas.android.com/apk/res/android">
  3. <itemandroid:state_window_focused="false"android:state_enabled="true"android:drawable="@drawable/handle_normal"/>
  4. <itemandroid:state_pressed="true"android:drawable="@drawable/handle_pressed"/>
  5. <itemandroid:state_focused="true"android:state_enabled="true"android:drawable="@drawable/handle_focused"/>
  6. <itemandroid:state_enabled="true"android:drawable="@drawable/handle_normal"/>
  7. <itemandroid: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布局:代码如下:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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. <?xmlversion="1.0"encoding="utf-8"?>
  2. <selectorxmlns:android="http://schemas.android.com/apk/res/android">
  3. <itemandroid:state_window_focused="false"android:state_enabled="true"android:drawable="@drawable/handle_normal"/>
  4. <itemandroid:state_pressed="true"android:drawable="@drawable/handle_pressed"/>
  5. <itemandroid:state_focused="true"android:state_enabled="true"android:drawable="@drawable/handle_focused"/>
  6. <itemandroid:state_enabled="true"android:drawable="@drawable/handle_normal"/>
  7. <itemandroid:state_focused="true"android:drawable="@drawable/handle_focused"/>
  8. </selector>

5.运行之。将会得到如下效果:

的比较简单呵呵,如果想深入了解,大家看Launcher源码吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值