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

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

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

<?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"
>
  <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    android:textSize="20sp"
  />
  <SlidingDrawer
    android:id="@+id/sd"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:handle="@+id/iv" 
    android:content="@+id/myContent"
    android:orientation="vertical"
  >

<ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/open1"
      />

<GridView
      android:id="@id/myContent"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:numColumns="3"
      android:background="@drawable/background"
      android:gravity="center"
    /> 
      
  </SlidingDrawer>
</RelativeLayout>

在SlidingDrawer这个标签下android:handle:指示的就是抽屉的图片。android:content:指向的就是抽屉里面的布局。有了这个布局,其实一个抽屉就出来了。

下面我们看Chouti这个类的代码

public class Chouti extends Activity {
 
  private GridView gv;
  private SlidingDrawer sd;
  private ImageView iv;
  private int[] icons={R.drawable.browser,R.drawable.gallery,
                        R.drawable.camera,R.drawable.gmail,
                        R.drawable.music,R.drawable.market,
                        R.drawable.phone,R.drawable.messages,R.drawable.maps};
  private String[] items={"浏览器","图片","相机","时钟","音乐","市场","拨号","信息","地图"};
     
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        gv = (GridView)findViewById(R.id.myContent);
        sd = (SlidingDrawer)findViewById(R.id.sd);
        iv=(ImageView)findViewById(R.id.iv);
        MyAdapter adapter=new MyAdapter(this,items,icons);//自定义MyAdapter来实现图标加item的显示效果
        gv.setAdapter(adapter);
        sd.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener()//开抽屉
        {
          @Override
          public void onDrawerOpened()
          {
            iv.setImageResource(R.drawable.close1);//响应开抽屉事件 ,把图片设为向下的
          }
        });
        sd.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener()
        {
          @Override
          public void onDrawerClosed()
          {
            iv.setImageResource(R.drawable.open1);//响应关抽屉事件
          }
        });
    }
}

在整个类里面将布局导入,同时设置开关抽屉的监听事件。这里面我们需要自定义一个MyAdapter来显示带文字下标的图片。

下面是MyAdapter这个类的代码

public class MyAdapter extends BaseAdapter

  private Context _ct;
  private String[] _items;
  private int[] _icons;

public MyAdapter(Context ct,String[] items,int[] icons) //构造器
  {
    _ct=ct;
    _items=items;
    _icons=icons;
  }

@Override
  public int getCount()
  {
    return _items.length;
  }

@Override
  public Object getItem(int arg0)
  {
    return _items[arg0];
  }

@Override
  public long getItemId(int position)
  {
    return position;
  }

@Override
  public View getView(int position, View convertView, ViewGroup parent)
  {
    LayoutInflater factory = LayoutInflater.from(_ct);
    View v = (View) factory.inflate(R.layout.gv, null);//绑定自定义的layout
    ImageView iv = (ImageView) v.findViewById(R.id.icon);
    TextView tv = (TextView) v.findViewById(R.id.text);
    iv.setImageResource(_icons[position]);
    tv.setText(_items[position]);
    return v;
  }
}

也是非常的简单,其中用到的布局如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns: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="40px"
    android:layout_gravity="center"
  />
  <TextView 
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textColor="#ffffffff"
  />
</LinearLayout>

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

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

转载于:https://www.cnblogs.com/noTice520/archive/2011/01/27/1946343.html

android UI进阶之android中隐藏的layout 抽屉的运用相关推荐

  1. 腾讯 android ui,腾讯开源的Android UI框架——QMUI Android

    各位同学,早上好,我是你们的老朋友D_clock爱吃葱花,前些天忙着发版本,最近也在看各种各样的新知识,有好多东西想写啊啊啊啊啊.嗯,先冷静捋一下,卖个关子.扯回正题,今天继续为大家推荐一个Githu ...

  2. android UI进阶之实现listview中checkbox的多选与记录

    今天继续和大家分享涉及到listview的内容.在很多时候,我们会用到listview和checkbox配合来提供给用户一些选择操作.比如在一个 清单页面,我们需要记录用户勾选了哪些条目.这个的实现并 ...

  3. android UI进阶之布局的优化(二)

    上一篇博客中介绍了布局优化的工具,层级观察器Hierarchy Viewer和布局优化分析工具layoutopt.如果看过上篇博客的会注意到,layoutopt工具提示可以将<FrameLayo ...

  4. (转) android UI进阶之用gallery实现可滑动的Tab

    首先我们需要写Gallery的适配器.这里我们要注意的是Gallery有一个特点,就是起始一个元素的左边会留下一块空位,如下图所示: 这样我们的Tab显然不是很完美,如何解决?开始想的就是去看gall ...

  5. (转)android UI进阶之自定义组合控件

    第一个实现一个带图片和文字的按钮,如图所示: 整个过程可以分四步走.第一步,定义一个layout,实现按钮内部的布局.代码如下: [html] view plaincopy <?xml vers ...

  6. android UI进阶之布局的优化

    好久没更新博客了,趁着清明来写点什么. 今天来讲下如何使用android中提供的工具优化我们的布局.首先我们写一个最简单的框架布局. <?xml version="1.0"  ...

  7. (转)android UI进阶之用ViewPager实现欢迎引导页面

    ViewPager需要android-support-v4.jar这个包的支持,来自google提供的一个附加包.大家搜下即可. ViewPager主要用来组织一组数据,并且通过左右滑动的方式来展示. ...

  8. android UI进阶之实现listview的分页加载

    上篇博文和大家分享了下拉刷新,这是一个用户体验非常好的操作方式.新浪微薄就是使用这种方式的典型. 还有个问题,当用户从网络上读取微薄的时候,如果一下子全部加载用户未读的微薄这将耗费比较长的时间,造成不 ...

  9. android UI进阶之仿iphone的tab效果

    相信很多人都喜欢iphone 酷炫的界面,虽然android的原生控件已经足够漂亮,但是往往不能满足用户越来越挑剔的眼光.其实,我们完全可以自己来绘制界面.今天我就来分享下做一个和iphone一样的t ...

  10. Android UI进阶之旅3 Material Design之侧滑菜单的两种实现

    ###侧滑菜单的两种实现 使用DrawerLayout,灵活度比较高. 使用DrawerLayout+NavigationView,这是谷歌对Material Design的一种标准化. ###使用D ...

最新文章

  1. 线段树 ---- 2021牛客多校第一场 J Journey among Railway Stations [线段树维护区间可行性判断]
  2. 我们的故事(八)-----仲夏夜之梦
  3. c++错误functional:1526:9: error: no type named ‘type’ in ‘class std::result_of
  4. Marlin Protocol正在为验证者开放OTC渠道
  5. POCO库中文编程参考指南(11)如何使用Reactor框架?
  6. php调用成员函数错误,PHP致命错误:在非对象上调用成员函数exec...
  7. 看老友记_马云对话周星驰
  8. Alex and broken contest (字符串)CodeForces - 877A
  9. C# 判断时间是否在 某一时间段内,判断时间是否是今天,获取今年第一天、最后一天,数字字符串转换为日期
  10. 【深度学习之美笔记】人工“碳”索意犹尽,智能“硅”来未可知(入门系列之二)
  11. 根据银行卡号获取银行名称、编码、类型
  12. JPG格式图片怎么减小体积?一招教你轻松压缩JPG图片
  13. python selenium 爬虫遇到 由于目标计算机积极拒绝,无法连接
  14. YTU 问题 : 数组奇偶操作
  15. elementui的使用问题
  16. html页面文字随机效果,教你用javascript实现随机标签云效果_附代码
  17. 面向对象分析与设计01 - 关键抽象
  18. javascript 应用
  19. 2017百度实习生招聘编程题
  20. 2022年度软件质量保障行业调查报告

热门文章

  1. Silverlight for Windows Phone 开发学习笔记(-)
  2. Springboot项目使用aop添加日志
  3. Django之HttpRequest和HttpReponse
  4. python-django rest framework框架之渲染器
  5. struts转换器详解
  6. Prism初研究之依赖管理
  7. SQL 查询CET使用领悟
  8. C# 随机数 Radom 循环生成同一的数字
  9. iOS xcode ‘XXXX’ was compiled with optimization - stepping may behave oddly; variables may not be av
  10. Python接口自动化实战(第二阶段)- unittest框架