SlidingDrawer类使用的时候主要包括两点: 
1:handle:单击的按钮

2:content:抽屉中的内容,单击按钮时,抽屉的内容隐藏或显示

下面这段xml布局是摘自Google SDK帮助文档:

<SlidingDrawerandroid:id="@+id/drawer"android:layout_width="match_parent"android:layout_height="match_parent"android:handle="@+id/handle"android:content="@+id/content"><ImageViewandroid:id="@id/handle"android:layout_width="88dip"android:layout_height="44dip" /><GridViewandroid:id="@id/content"android:layout_width="match_parent"android:layout_height="match_parent" /></SlidingDrawer>

下面给出一个实例

首先实例的实现截图如下:

实现滑动效果

给出如下实现代码:

1.总体布局文件的代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"
><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello"android:textSize="16sp"/><SlidingDrawer android:id="@+id/drawer1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:handle="@+id/layout1" android:content="@+id/myContent1" android:orientation="horizontal"><LinearLayout android:id="@id/layout1" android:layout_width="35px" android:layout_height="fill_parent"android:background="@drawable/black"android:gravity="center" ><ImageViewandroid:id="@+id/myImage1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/open"/></LinearLayout><GridView android:id="@id/myContent1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numColumns="2"android:background="@drawable/black" android:gravity="center"/>       </SlidingDrawer>
</RelativeLayout>

2.GridView中的布局代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"
><ImageView android:id="@+id/icon"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"/><TextView android:id="@+id/text"android:layout_width="fill_parent"android:layout_height="20sp"android:gravity="center"android:textColor="@drawable/white"/>
</LinearLayout>

3.BaseAdapter的子类实现(为GridView提供数据)

/* 自定义Adapter,继承BaseAdapter */
public class MyGridViewAdapter extends BaseAdapter
{ private Context _con;private String[] _items;private int[] _icons;/* 构造符 */public MyGridViewAdapter(Context con,String[] items,int[] icons) {_con=con;_items=items;_icons=icons;}@Overridepublic int getCount(){return _items.length;}@Overridepublic Object getItem(int arg0){return _items[arg0];}@Overridepublic long getItemId(int position){return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent){LayoutInflater factory = LayoutInflater.from(_con);/* 使用grid.xml为每几个item的Layout */View v = (View) factory.inflate(R.layout.grid, null);/* 取得View */ImageView iv = (ImageView) v.findViewById(R.id.icon);TextView tv = (TextView) v.findViewById(R.id.text);/* 设定显示的Image与文? */iv.setImageResource(_icons[position]);tv.setText(_items[position]);return v;}
}

4.主程序的实现

public class EX04_27 extends Activity
{private GridView gv;private SlidingDrawer sd;private ImageView im;private int[] icons={R.drawable.alarm,R.drawable.calendar,R.drawable.camera,R.drawable.clock,R.drawable.music,R.drawable.tv};private String[] items={"Alarm","Calendar","Camera","Clock","Music","TV"};/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);/* 加载main.xml Layout */setContentView(R.layout.main);/* 初始化对象 */gv = (GridView)findViewById(R.id.myContent1); sd = (SlidingDrawer)findViewById(R.id.drawer1);im=(ImageView)findViewById(R.id.myImage1);/* 使用告定义的MyGridViewAdapter设置GridView里面的item内容 */MyGridViewAdapter adapter=new MyGridViewAdapter(this,items,icons);gv.setAdapter(adapter);/* 设定SlidingDrawer被打开的事件处理 */sd.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener(){@Overridepublic void onDrawerOpened(){im.setImageResource(R.drawable.close);}});/* 设置SlidingDrawer被关闭的事件处理 */sd.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener(){@Overridepublic void onDrawerClosed(){im.setImageResource(R.drawable.open);}});}
}

Android--SlidingDrawer的使用介绍相关推荐

  1. Xamarin Android教程Android基本知识版本介绍与系统介绍

    Xamarin Android教程Android基本知识版本介绍与系统介绍 Xamarin Android教程Android基本知识版本介绍与系统介绍,开发Andriod有时候不像iOS一样轻松,因为 ...

  2. Android MVP模式 简单易懂的介绍方式

    主要学习这位大神的博客:简而易懂 Android MVP模式 简单易懂的介绍方式 https://segmentfault.com/a/1190000003927200 转载于:https://www ...

  3. Android SQLite 数据库详细介绍

    Android SQLite 数据库详细介绍我们在编写数据库应用软件时,需要考虑这样的问题:因为我们开发的软件可能会安装在很多用户的手机上,如果应用使用到了SQLite数据库,我们必须在用户初次使用软 ...

  4. Android MVP模式简单易懂的介绍方式 (一)

    Android MVP模式简单易懂的介绍方式 (一) Android MVP模式简单易懂的介绍方式 (二) Android MVP模式简单易懂的介绍方式 (三) 最近正在研究Android的MVP模式 ...

  5. Android Studio Text组件介绍

    Android Studio Text组件介绍 Text组件 1.1Text组件有那些 以上就是常见的text组件 别看这么多,其实大体上仅需分为6种: 稍微介绍一下 如何使用?上代码! <&g ...

  6. ABD — android debug bridge 简略介绍

    ABD - android debug bridge 简略介绍 文章目录 ABD - android debug bridge 简略介绍 一.adb是什么? 二.组成部分 三.adb 常用命令 四.总 ...

  7. Android 6.0 Marshmallow介绍

    Android 6.0 Marshmallow介绍 Android 6.0 (M) 为用户和应用开发者提供了新功能.本文旨在介绍其中最值得关注的 API和重点介绍您应该了解并在开发应用时加以考虑的一些 ...

  8. 2018年android常用的框架介绍

    转载地址:http://blog.csdn.net/RuingMan/article/details/73546718 http://www.cnblogs.com/jincheng-yangchao ...

  9. Android手机凡客项目介绍

    Android手机凡客项目介绍 凡客诚品Android客户端是凡客诚品专为Android用户免费定制的购物软件,它与凡客网上万产品库对接,拥有商品搜索.浏览.收藏.查物流.购物车等功能,保持用户的体验 ...

  10. android NSD service使用介绍

    android NSD service使用介绍 最近项目中要实现局域网设备发现,接触到了MDNS协议,什么是MDNS协议呢,下面摘抄自百度百科. mDNS即组播DNS(multicast DNS).使 ...

最新文章

  1. xamarin 断点 不命中
  2. Centos ab测试工具
  3. HibernateTemplate 的常见用法
  4. 微会动微信现场互动:如何通过7步分析法建立用户视角?
  5. Selector 概念
  6. MATLAB入门学习(三)
  7. freeradius 3.0 时间限制_创意营销3.0新模式下,易企秀要成为中国的Adobe
  8. spring-in-action-mvc-jdbc搭建工程
  9. 关于建立 Carbon Forum 后 设置访问链接的问题
  10. labview曲线上两点画延长线_零失手的‘万能眼线公式’,关键鼻翼延长线、画出适合自己的眼线...
  11. JDK动态代理在RPC框架中的应用
  12. 二维数组代表迷宫java_Java 二维数组迷宫游戏
  13. java list remove 无效_JAVA List使用Remove时的一些问题
  14. HTMLCSS入门学习
  15. php怎么把两个字符转成hex,PHP字符串函数bin2hex()(将字符串转换为十六进制值)
  16. vue运行之神奇的npm install --legacy-peer-deps
  17. java获取上周一_java如何得到上周一和上周日的日期
  18. 索骥馆-OFFICE系列之《北风网Excel高端应用培训:多条件约束报表自动统计系统分析与制作》共4章更新完
  19. 开关稳压电源和线性稳压电源
  20. STM32G070芯片开发笔记:一、如何用ST送的学习板上的STLink烧录调试

热门文章

  1. CDQZ集训DAY8 日记
  2. PS如何批量生成缩略图(方法可以通用其他重复劳动)
  3. Apache 超详细编译参数解析
  4. bash shell命令(1)
  5. ubuntu 源更新(sources.list)
  6. PermGen space错误解决方法
  7. nginx 配置文件
  8. 探讨DHCP之一:了解DHCP
  9. [SQL Server]Index/deadlock
  10. hibernate select 读取结果集的两种办法