摘自:
http://rayloo.iteye.com/blog/1010896
http://wenku.baidu.com/view/6edff2d950e2524de5187ec1.html
一、简介
Sliding Drawer 隐藏屏外的内容,并允许用户通过handle以显示隐藏内容。它可以垂直或水平滑动,它有俩个View组成,其一是可以拖动的handle,其二是隐藏内容的View.它里面的控件必须设置布局,在布局文件中必须指定handle和content。
< SlidingDrawer android:layout_width="fill_parent"
android:layout_height="fill_parent" android:handle="@+id/handle"
android:content="@+id/content" android:orientation="vertical"
android:id="@+id/slidingdrawer">
< ImageButton android:id="@id/handle" android:layout_width="50dip"
android:layout_height="44dip" android:src="@drawable/up" />
< LinearLayout android:id="@id/content"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="#ffffff">
< TextView android:text="这是一个滑动式抽屉的示例"
android:id="@+id/tv"
android:textSize="18px"
android:textColor="#000000"
android:gravity="center_vertical|center_horizontal"
android:layout_width="match_parent"
android:textStyle="bold"
android:layout_height="match_parent">
< /TextView>
</LinearLayout>
</SlidingDrawer>
二、重要属性
android:allowSingleTap:指示是否可以通过handle打开或关闭
android:animateOnClick:指示是否当使用者按下手柄打开/关闭时是否该有一个动画。
android:content:隐藏的内容
android:handle:handle(手柄)
三、重要方法
animateClose():关闭时实现动画。
close():即时关闭
getContent():获取内容
isMoving():指示SlidingDrawer是否在移动。
isOpened():指示SlidingDrawer是否已全部打开
lock():屏蔽触摸事件。
setOnDrawerCloseListener(SlidingDrawer.OnDrawerCloseListener onDrawerCloseListener):SlidingDrawer关闭时调用
unlock():解除屏蔽触摸事件。
toggle():切换打开和关闭的抽屉SlidingDrawer。
四、完整实例
1.布局文件slidingdrawer.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" android:background="@drawable/default_bg">
<SlidingDrawer android:layout_width="fill_parent"
android:layout_height="fill_parent" android:handle="@+id/handle"
android:content="@+id/content" android:orientation="vertical"
android:id="@+id/slidingdrawer">
<ImageButton android:id="@id/handle" android:layout_width="50dip"
android:layout_height="44dip" android:src="@drawable/up" />
<LinearLayout android:id="@id/content"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="#ffffff">
<TextView android:text="这是一个滑动式抽屉的示例"
android:id="@+id/tv"
android:textSize="18px"
android:textColor="#000000"
android:gravity="center_vertical|center_horizontal"
android:layout_width="match_parent"
android:textStyle="bold"
android:layout_height="match_parent"></TextView>
</LinearLayout>
</SlidingDrawer>
</LinearLayout>
2.Java代码
package com.wjq;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.SlidingDrawer;
import android.widget.TextView;
public class SlidingDrawerDemo extends Activity {
private SlidingDrawer mDrawer;
private ImageButton imbg;
private Boolean flag=false;
private TextView tv;
/* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sildingdrawer);
imbg=(ImageButton)findViewById(R.id.handle);
mDrawer=(SlidingDrawer)findViewById(R.id.slidingdrawer);
tv=(TextView)findViewById(R.id.tv);
mDrawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener(){
@Override
public void onDrawerOpened() {
flag=true;
imbg.setImageResource(R.drawable.down);
}
});
mDrawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener(){
@Override
public void onDrawerClosed() {
flag=false;
imbg.setImageResource(R.drawable.up);
}
});
mDrawer.setOnDrawerScrollListener(new SlidingDrawer.OnDrawerScrollListener(){
@Override
public void onScrollEnded() {
tv.setText("结束拖动");
}
@Override
public void onScrollStarted() {
tv.setText("开始拖动");
}
});
}
}
另外两篇:
android SlidingDrawer(抽屉)
图中就是抽屉打开之后的效果,里面列出来了所有的正在运行的程序。点击可以关闭,点击骷髅头可以关闭所有正在运行的程序!
下面是布局文件:
<SlidingDrawer
android:id="@+id/run_app"
android:layout_width="555dip"
android:layout_height="56dip"
android:layout_gravity="right|center"
android:layout_marginTop="00dip"
android:orientation="horizontal"
android:handle="@+id/handle"
android:content="@+id/all_run_app">
<ImageView
android:id="@+id/handle"
android:src="@drawable/death"
android:layout_width="wrap_content"
android:layout_height="56dip"
/>
<LinearLayout
android:id="@+id/all_run_app"
android:orientation="horizontal"
android:background="#00000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="56dip"
android:background="@drawable/rounded_kill_img" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="38dip">
<ImageView
android:id="@+id/refresh_all"
android:src="@drawable/refresh_press"
android:layout_width="wrap_content"
android:layout_height="38dip"/>
<ImageView
android:id="@+id/kill_all"
android:src="@drawable/kill_all_press"
android:layout_width="wrap_content"
android:layout_height="38dip"/>
</LinearLayout>
<TextView
android:id="@+id/show_mem"
android:layout_width="wrap_content"
android:layout_height="18dip"
android:textSize="12dip"/>
</LinearLayout>
<GridView
android:id="@+id/grid_all"
android:numColumns="12"
android:background="@drawable/rounded_kill_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</SlidingDrawer>
注意:
1、如果给SlidingDrawer添加android:clickable="true"则在抽屉活动的一块区域屏幕不会响应其他点击事件,即使抽屉式关闭的。
2、android:handle="@+id/handle"里面的handle是SlidingDrawer标签里面你要作为抽屉图标的view的id。android:content="@+id/all_run_app"中的all_run_app是SlidingDrawer标签里面你要作为抽屉里面内容显示的view布局的id。
3、上面的android:background="@drawable/rounded_kill_img"这句就是指定图中的圆角半透明背景的布局文件,我博客之前的一片文章有介绍这个!
4、android:numColumns="12"是指定GridView中一行所显示的数量。
其他就是在代码中初始化每个view,并为其点击事件添加相应的功能代码!
下面贴一段结束应用程序的代码:
private List<String> processName = new ArrayList<String>();
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (String pName : processName) {
manager.restartPackage(pName);
}
注意:restartPackage(pName);这个方法在2.1上可以结束一切应用程序,但在2.2上只能结束部分程序。关于结束本进程以及其他程序或进程大家可以参考这儿:http://www.cnblogs.com/crazypebble/archive/2011/04/05/2006213.html
http://h3clikejava.blog.163.com/blog/static/134473029201112083447207/
如果抽屉下方的布局是listview等,在使用了LinearLayout时抽屉是不会显示在这些view的上面的。为什么呢?
参考了这儿:
http://www.eoeandroid.com/forum-viewthread-tid-62987-highlight-SlidingDrawer.html
将最外层的布局形式改成了FrameLayout ,问题是解决了。
SlidingDrawer的使用
SlideDrawer是多个(两个页面)的一种显示方式。如上左图所示,普通的,我们显示Hello的Label,当我们按下面的SlidingDrawer的ImageView,即右图所示图标时,可以将SlideDrawer的内容显示上去,如中图。SlidingDrawer可以在Open和Close两个状态之间切换。Open时覆盖,不是所有的Layout都能支持这种叠加覆盖,作为SlidingDrawer的container,例如LinearLayout就不可以,一般使用RelativeLayout和FrameLayout。下面是例子的XML文件,采用了FrameLayout作为容器:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF8888CC ">
<TextView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textStyle="bold"
android:gravity="center"
android:text="Hello" /> <!-- 我们采用了简单的TextView表示原本页的内容,在SlidingDrawer处于Close时显示,如左图-->
<SlidingDrawer android:id="@+id/c96_drawer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
<!-- SlidingDrawer中包含两个重要参数handle和content,类似于Tab的标签和内容,我们需要在这里设定相关的id,以便引用-->
android:handle="@+id/c96_handle"
android:content="@+id/c96_content" >
<ImageView android:id="@id/c96_handle"<!-- 和SlidingDrawer的handle的id相一致,一般而言会是一个Image,当然我们也可以设置为Button等其他,只是美观上欠佳。-->
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/tray_handle_normal" />
<Button android:id="@id/c96_content" <!-- 和SlidingDrawer的content的id相一致,这里我们简单用Button来处理-->
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="I am in Here" />
</SlidingDrawer >
</FrameLayout>
源文件无须特别处理,对于SlidingDrawer,可以open(), close() , toggle() ,也可以采用动画的方式anmiateOpen(),animateClose()和animateToggle()。可以在XML中设置android:animateOnClick ="true" ,缺省为真。
我们还可以设置lock()和unlock()。
我们可以对SlidingDrawer的事件进行监听:openen,closed,scrolled(用户拖拽标签)。例如:setOnDrawerCloseListener (SlidingDrawer.OnDrawerCloseListener onDrawerCloseListener)。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/flowingflying/archive/2011/04/06/6305184.aspx