Android从零开始(十七)

本文介绍如何使用Android的SlidingDrawer组件实现一个可滑动展开的菜单效果,并提供了详细的布局配置及图标切换代码。

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

知识点:抽屉(SlidingDrawer)

先看一下抽屉打开之前的效果图:

[img]http://dl.iteye.com/upload/attachment/0080/8932/bc7f7124-5c63-3098-9f3d-f22dc8023838.png[/img]

抽屉打开之后的效果图:


[img]http://dl.iteye.com/upload/attachment/0080/8934/9bc07442-aebf-3b76-bd15-c69adb98eb3a.png[/img]


步骤一、抽屉最重要的是布局(核心代码如下)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<SlidingDrawer
android:layout_width="fill_parent"
android:layout_height="250dp"
android:id="@+id/slidingDrawer"
android:handle="@+id/myHandle"
android:content="@+id/myContent"
android:layout_alignParentBottom="true"
>
<!-- 抽屉把手 -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/open"
android:id="@id/myHandle"
/>
<!-- 抽屉内容 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@id/myContent"
android:orientation="vertical"
android:background="@drawable/background"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/insert_btn"
android:src="@drawable/insert"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/upload_btn"
android:src="@drawable/upload"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/exit_btn"
android:src="@drawable/exit"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/backup_btn"
android:src="@drawable/backup"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/help_btn"
android:src="@drawable/help"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/about_btn"
android:src="@drawable/about"
/>
</LinearLayout>
</LinearLayout>
</SlidingDrawer>

</RelativeLayout>


步骤二、设置打开和关闭图标(核心代码如下)
package com.veryedu.slidingdrawer;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.SlidingDrawer;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

getActionBar().setDisplayHomeAsUpEnabled(true);
SlidingDrawer slidingDrawer=(SlidingDrawer)findViewById(R.id.slidingDrawer);
slidingDrawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {

@Override
public void onDrawerOpened() {
((ImageView)findViewById(R.id.myHandle)).setImageResource(R.drawable.close);

}
});
slidingDrawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {

@Override
public void onDrawerClosed() {
// TODO Auto-generated method stub
((ImageView)findViewById(R.id.myHandle)).setImageResource(R.drawable.open);

}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}


源码下载请点击这里:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值