Android 控件使用之SlidingDrawer

SlidingDrawer 就是Android的抽屉类。它的一个很明显的使用的情况是在Launcher中的效果。可以自己查看Android的源代码来。

这里就简单的说下使用方法:

首先在主布局文件里面,写入如下代码:然后再来解释为什么这样写:

view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?>    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
        android:orientation="horizontal"    
        android:layout_width="fill_parent"    
        android:layout_height="fill_parent"    
        android:background="#808080"    
        >    
    <SlidingDrawer    
        android:id="@+id/slidingdrawer"    
       android:layout_width="fill_parent"    
       android:layout_height="fill_parent"    
       android:orientation="horizontal"    
       android:handle="@+id/handle"    
       android:content="@+id/content">    
       <Button    
               android:id="@+id/handle"    
               android:layout_width="wrap_content"    
               android:layout_height="wrap_content"    
               android:background="@drawable/handle"    
           />    
       <LinearLayout    
           android:id="@+id/content"    
           android:layout_width="fill_parent"    
           android:layout_height="fill_parent"    
           android:background="#00ff00">    
           <Button    
               android:id="@+id/button"    
               android:layout_width="wrap_content"    
               android:layout_height="wrap_content"    
               android:text="Button"    
           />    
          <EditText    
               android:id="@+id/editText"    
               android:layout_width="fill_parent"    
               android:layout_height="wrap_content"    
           />    
       </LinearLayout>    
   </SlidingDrawer>    
   </LinearLayout>   
<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
        android:orientation="horizontal" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:background="#808080" 
        > 
    <SlidingDrawer 
        android:id="@+id/slidingdrawer" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="horizontal" 
       android:handle="@+id/handle" 
       android:content="@+id/content"> 
       <Button 
               android:id="@+id/handle" 
               android:layout_width="wrap_content" 
               android:layout_height="wrap_content" 
               android:background="@drawable/handle" 
           /> 
       <LinearLayout 
           android:id="@+id/content" 
           android:layout_width="fill_parent" 
           android:layout_height="fill_parent" 
           android:background="#00ff00"> 
           <Button 
               android:id="@+id/button" 
               android:layout_width="wrap_content" 
               android:layout_height="wrap_content" 
               android:text="Button" 
           /> 
          <EditText 
               android:id="@+id/editText" 
               android:layout_width="fill_parent" 
               android:layout_height="wrap_content" 
           /> 
       </LinearLayout> 
   </SlidingDrawer> 
   </LinearLayout>  

SlidingDrawer无非就是一个控件,可以在DOC中查看它的属性,说明如下:

SlidingDrawer hides content out of the screen and allows the user to drag a handle to bring the content on screen. SlidingDrawer can be used vertically or horizontally. A special widget composed of two children views: the handle, that the users drags, and the content, attached to the handle and dragged with it. SlidingDrawer should be used as an overlay inside layouts. This means SlidingDrawer should only be used inside of a FrameLayout or a RelativeLayout for instance. The size of the SlidingDrawer defines how much space the content will occupy once slid out so SlidingDrawer should usually use fill_parent for both its dimensions. Inside an XML layout, SlidingDrawer must define the id of the handle and of the content:

这个里面我们有两个子控件,一个是handle, 一个是Content,其实也是可以重复叠加的。

然后设置handle  图标的样式,在drawable  里添加handle.xml  ,代码如下:

view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?>    
<selector xmlns:android="http://schemas.android.com/apk/res/android">    
    <item android:state_window_focused="false" android:state_enabled="true" android:drawable="@drawable/handle_normal" />    
    <item android:state_pressed="true" android:drawable="@drawable/handle_pressed" />    
    <item android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/handle_focused" />    
    <item android:state_enabled="true" android:drawable="@drawable/handle_normal" />    
    <item android:state_focused="true" android:drawable="@drawable/handle_focused" />    
</selector> 
<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_window_focused="false" android:state_enabled="true" android:drawable="@drawable/handle_normal" /> 
    <item android:state_pressed="true" android:drawable="@drawable/handle_pressed" /> 
    <item android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/handle_focused" /> 
    <item android:state_enabled="true" android:drawable="@drawable/handle_normal" /> 
    <item android:state_focused="true" android:drawable="@drawable/handle_focused" /> 
</selector>

Android通过selector改变界面状态 ,它也就是一个资源描述文件。我们在drawable文件夹下面放了三个图片,用来描述按键的状态。
  实际上这样就完成了一个Demo的操作,如果还需要更多的细节,可以参见Launcher的代码。

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/dadahacker/archive/2010/07/10/5724845.aspx

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值