先看一下抽屉打开之前的效果图:
抽屉打开之后的效果图:
步骤一、抽屉最重要的是布局(核心代码如下)
<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; } }
源码下载请点击这里: