一、简介
SlidingDrawer隐藏屏外的内容,并允许用户通过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:> < ImageButton android: android:layout_width="50dip" android:layout_height="44dip" android:src="@drawable/up" /> < LinearLayout android: android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff"> < TextView android:text="这是一个滑动式抽屉的示例" android: 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>二、重要属性
<?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("开始拖动"); } }); } }Open时覆盖,不是所有的Layout都能支持这种叠加覆盖,作为SlidingDrawer的container,例如LinearLayout就不可以,一般使用RelativeLayout和FrameLayout。