android之SlidingDrawer实现抽屉效果
通常我们需要在用户的屏幕上展示更多的信息,但是用户的屏幕大小是有限的,那么我们如何利用有限的空间来展示更多的信息呢?Android为我们提供了SlidingDrawer类,帮助我们轻松实现想要的效果。诸如在墨迹天气中的墨迹推荐,360安全卫士中都有所体现。下面我们就来学习SlidingDrawer类,实现上述效果:
效果图:
未展开时:
展开后:
下面看代码:
首先是布局文件:
[html] <?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"
android:background="@drawable/splash_background"
>
<SlidingDrawer android:id="@+id/slidingDrawer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:handle="@+id/handle"
android:content="@+id/content"
>
<LinearLayout android:id="@+id/content"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/empty">
</LinearLayout>
<Button android:id="@+id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" open "
android:textSize="24dip"
/>
</SlidingDrawer>
</LinearLayout>
<?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"
android:background="@drawable/splash_background"
>
<SlidingDrawer android:id="@+id/slidingDrawer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:handle="@+id/handle"
android:content="@+id/content"
>
<LinearLayout android:id="@+id/content"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/empty">
</LinearLayout>
<Button android:id="@+id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" open "
android:textSize="24dip"
/>
</SlidingDrawer>
</LinearLayout>
主要属性:
android:handle="@+id/handle" 相当于展开或收回按钮,这里我用了一个Button来演示(TextView等都可以),在实际项目中使用ImageView比较好,只需设置展开和关闭时的图标样式就可以了。
android:content="@+id/content" 展开后增加的内容,没什么好说的。
主代码:
[java] package com.cloay.slidingdrawer;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;
/**
* 抽屉效果演示
* SlidingDrawerTestActivity.java
* @author cloay
* 2012-1-30
*/
public class SlidingDrawerTestActivity extends Activity {
/** Called when the activity is first created. */
private Button button;
private SlidingDrawer slidingDrawer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.handle);
slidingDrawer = (SlidingDrawer) findViewById(R.id.slidingDrawer);
slidingDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener() {//收回做一些处理
@Override
public void onDrawerClosed() {
button.setText("Open"); //这里我将文字提示改成Open
}
});
slidingDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {//展开时做一些处理
@Override
public void onDrawerOpened() {
button.setText("Close"); //这里我将文字提示改成Close
补充:移动开发 , Android ,