最近在写一个应用,想把设置页面和应用页面放在一起,这样就能实现用户可以实时看到自己的设置对UI的影响,从而更方便的设置用户喜欢的界面。想了一段时间,发现用slidingDrawer这个控件可以实现这个效果。也就是一个抽屉。拉开抽屉,占据半个屏幕,另外半个屏幕还是显示应用页面。效果还是不错的。

今天就和大家分享一下android中这个抽屉效果。其实在android的lanucher就是一个抽屉,打开它就可以看到安装的应用。相信大家都见过用过。下面我们就来做个相同的效果,当然只是UI上差不多相同的效果。

slidingDrawer这个控件使用非常简单,基本在xml里面配置就可以。代码如下所示。

        
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/hello"
  11. android:textSize="20sp"
  12. />
  13. <SlidingDrawer
  14. android:id="@+id/sd"
  15. android:layout_width="match_parent"
  16. android:layout_height="match_parent"
  17. android:handle="@+id/iv"
  18. android:content="@+id/myContent"
  19. android:orientation="vertical"
  20. >
  21. <ImageView
  22. android:id="@+id/iv"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:src="@drawable/open1"
  26. />
  27. <GridView
  28. android:id="@id/myContent"
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:numColumns="3"
  32. android:background="@drawable/background"
  33. android:gravity="center"
  34. />
  35. </SlidingDrawer>
  36. </RelativeLayout>
  37. 在SlidingDrawer这个标签下android:handle:指示的就是抽屉的图片。android:content:指向的就是抽屉里面的布局。有了这个布局,其实一个抽屉就出来了。
  38. 下面我们看Chouti这个类的代码
  39. publicclassChoutiextendsActivity{
  40. privateGridViewgv;
  41. privateSlidingDrawersd;
  42. privateImageViewiv;
  43. privateint[]icons={R.drawable.browser,R.drawable.gallery,
  44. R.drawable.camera,R.drawable.gmail,
  45. R.drawable.music,R.drawable.market,
  46. R.drawable.phone,R.drawable.messages,R.drawable.maps};
  47. privateString[]items={"浏览器","图片","相机","时钟","音乐","市场","拨号","信息","地图"};
  48. /**Calledwhentheactivityisfirstcreated.*/
  49. @Override
  50. publicvoidonCreate(BundlesavedInstanceState){
  51. super.onCreate(savedInstanceState);
  52. setContentView(R.layout.main);
  53. gv=(GridView)findViewById(R.id.myContent);
  54. sd=(SlidingDrawer)findViewById(R.id.sd);
  55. iv=(ImageView)findViewById(R.id.iv);
  56. MyAdapteradapter=newMyAdapter(this,items,icons);//自定义MyAdapter来实现图标加item的显示效果
  57. gv.setAdapter(adapter);
  58. sd.setOnDrawerOpenListener(newSlidingDrawer.OnDrawerOpenListener()//开抽屉
  59. {
  60. @Override
  61. publicvoidonDrawerOpened()
  62. {
  63. iv.setImageResource(R.drawable.close1);//响应开抽屉事件,把图片设为向下的
  64. }
  65. });
  66. sd.setOnDrawerCloseListener(newSlidingDrawer.OnDrawerCloseListener()
  67. {
  68. @Override
  69. publicvoidonDrawerClosed()
  70. {
  71. iv.setImageResource(R.drawable.open1);//响应关抽屉事件
  72. }
  73. });
  74. }
  75. }
  76. 在整个类里面将布局导入,同时设置开关抽屉的监听事件。这里面我们需要自定义一个MyAdapter来显示带文字下标的图片。
  77. 下面是MyAdapter这个类的代码
  78. publicclassMyAdapterextendsBaseAdapter
  79. {
  80. privateContext_ct;
  81. privateString[]_items;
  82. privateint[]_icons;
  83. publicMyAdapter(Contextct,String[]items,int[]icons)//构造器
  84. {
  85. _ct=ct;
  86. _items=items;
  87. _icons=icons;
  88. }
  89. @Override
  90. publicintgetCount()
  91. {
  92. return_items.length;
  93. }
  94. @Override
  95. publicObjectgetItem(intarg0)
  96. {
  97. return_items[arg0];
  98. }
  99. @Override
  100. publiclonggetItemId(intposition)
  101. {
  102. returnposition;
  103. }
  104. @Override
  105. publicViewgetView(intposition,ViewconvertView,ViewGroupparent)
  106. {
  107. LayoutInflaterfactory=LayoutInflater.from(_ct);
  108. Viewv=(View)factory.inflate(R.layout.gv,null);//绑定自定义的layout
  109. ImageViewiv=(ImageView)v.findViewById(R.id.icon);
  110. TextViewtv=(TextView)v.findViewById(R.id.text);
  111. iv.setImageResource(_icons[position]);
  112. tv.setText(_items[position]);
  113. returnv;
  114. }
  115. }
  116. 也是非常的简单,其中用到的布局如下
  117. <?xmlversion="1.0"encoding="utf-8"?>
  118. <LinearLayout
  119. xmlns:android="http://schemas.android.com/apk/res/android"
  120. android:orientation="vertical"
  121. android:layout_width="fill_parent"
  122. android:layout_height="fill_parent"
  123. >
  124. <ImageView
  125. android:id="@+id/icon"
  126. android:layout_width="wrap_content"
  127. android:layout_height="40px"
  128. android:layout_gravity="center"
  129. />
  130. <TextView
  131. android:id="@+id/text"
  132. android:layout_width="fill_parent"
  133. android:layout_height="wrap_content"
  134. android:gravity="center"
  135. android:textColor="#ffffffff"
  136. />
  137. </LinearLayout>

这样,我们的抽屉就完成啦 来看下效果 之前不能看到图 现在应该能了吧

就写这么多啦。抽屉这个控件非常实用,除了我在开头所说的我在程序中的应用外,还有很多的用途, 发挥你的想象力,抽屉将为你的应用增色不少。

更多相关文章

  1. Android实现伸缩弹力分布菜单效果
  2. 如何将Android应用发布到Google Play(Android(安卓)Market)官方市
  3. [置顶] 我的Android进阶之旅------>Android疯狂连连看游戏的实现
  4. 2020年了,Android后台保活还有戏吗?看我如何优雅的实现!
  5. Android大赛首轮获奖作品解析。。。
  6. 【一个android小应用的诞生全过程】【7.26-2011】
  7. 【幻灯片分享】凡客移动应用之Android(安卓)+ HTML5技术运用 |
  8. 如何发布你的Android应用程序
  9. Android(安卓)API Levels

随机推荐

  1. 小胖加入Android(安卓)Fans的 大军了 呵
  2. 在android 下支持ntfs-3g
  3. Android内核开发:源码的版本与分支详解
  4. Android导出一个JAR库/Android如何将程序
  5. 3G Android智能手机视频预览万里
  6. Android的Activity屏幕切换动画||Gesture
  7. 有关Android线程的学习
  8. Android中sqlite数据库的简单使用
  9. Android之Http网络编程(三)
  10. Android零基础入门第11节:简单几步带你飞,