Android控件之——SlidingDrawer的使用及重要方法

本文介绍了Android中的SlidingDrawer控件,它用于隐藏屏幕内容并允许用户通过拖动手柄显示内容。SlidingDrawer可垂直或水平使用,通常作为布局的覆盖层。内容包括SlidingDrawer的XML属性、布局示例以及关键方法的讨论,特别提到了其在Android 1.5应用程序列表中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们来看一下官方文档中对这个控件 的定义:

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 match_parent for both its dimensions. Inside an XML layout, SlidingDrawer must define the id of the handle and of the content:

其实android1.5的应用程序列表就应用了这个控件。

我们来看一下它特有的xml属性

XML Attributes
Attribute Name Related Method Description
android:allowSingleTap   Indicates whether the drawer can be opened/closed by a single tap on the handle. 
android:animateOnClick   Indicates whether the drawer should be opened/closed with an animation when the user clicks the handle. 
android:bottomOffset   Extra offset for the handle at the bottom of the SlidingDrawer. 
android:content   Identifier for the child that represents the drawer's content. 
android:handle   Identifier for the child that represents the drawer's handle. 
android:orientation   Orientation of the SlidingDrawer. 
android:topOffset   Extra offset for the handle at the top of the SlidingDrawer. 
这里都很清楚了,英文也很简单,我就不多说 了,直接上demo。

先来看一下最终的效果截个图:


两张图分别对应SlidingDrawer关闭时和打开时的视图


看一下layout的定义

<?xml version="1.0" encoding="utf-8"?>
<SlidingDrawer xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/drawer" android:layout_width="fill_parent"
	android:layout_height="fill_parent" android:handle="@+id/handle"
	android:content="@+id/content">

	<ImageView android:id="@id/handle" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:src="@drawable/up"></ImageView>
	<ListView android:id="@id/content" android:layout_width="fill_parent"
		android:layout_height="fill_parent" android:background="#ff00ff"></ListView>

</SlidingDrawer>

这里在SlidingDrawer元素里必须指定它的handle和content属性

再看一下Activity的实现:

package kevin.droid;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;
import android.widget.Toast;

public class SlidingDemo extends Activity implements OnItemClickListener,
		OnDrawerOpenListener, OnDrawerCloseListener {
	private SlidingDrawer drawer;
	private ImageView handle;
	private ListView content;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sliding);

		drawer = (SlidingDrawer) this.findViewById(R.id.drawer);
		handle = (ImageView) this.findViewById(R.id.handle);
		content = (ListView) this.findViewById(R.id.content);
		content.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, new String[] { "one",
						"two", "three" }));
		content.setOnItemClickListener(this);

		// 设置SlidingDrawer打开或者关闭时的监听器
		drawer.setOnDrawerOpenListener(this);
		drawer.setOnDrawerCloseListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Toast.makeText(this, "you clicked position " + (position + 1),
				Toast.LENGTH_SHORT).show();
	}

	// SlidingDrawer关闭时回调
	@Override
	public void onDrawerClosed() {
		handle.setImageResource(R.drawable.up);
	}

	// SlidingDrawer打开时回调
	@Override
	public void onDrawerOpened() {
		handle.setImageResource(R.drawable.down);
	}

}


它最重要的两个方法就是在代码中提到的

void setOnDrawerCloseListener( SlidingDrawer.OnDrawerCloseListener onDrawerCloseListener)
Sets the listener that receives a notification when the drawer becomes close.
void setOnDrawerOpenListener( SlidingDrawer.OnDrawerOpenListener onDrawerOpenListener)
Sets the listener that receives a notification when the drawer becomes open.
这是一个非常简单的demo,供刚开始学习Android的童鞋参考,已经有经验的童鞋请轻喷。。。


1/9 by KevinJom


评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值