//这个功能的实现 主要在布局里进行了配置
// 界面代码 利用监听setOnDrawerOpenListener 和 监听setOnDrawerCloseListener 开启与关闭
//详细代码如下:
<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"
android:background="@drawable/background"
tools:context=".MainActivity" >
<!--控件设置-->
<SlidingDrawer
android:id="@+id/slidingDrawer"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:content="@+id/content" // 绑定显示的能容 通过id进行绑定
android:handle="@+id/handle" //绑定把手 同样通过id绑定,如果没有绑定把手会报错
android:orientation="vertical"
>
<!-- 抽屉的把手部分 -->
<LinearLayout
android:id="@+id/handle"
android:layout_width="200dp"
android:layout_height="30dp"
android:orientation="vertical"
android:gravity="center"
>
<ImageButton
android:id="@+id/ib"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/open"
android:onClick="click"
/>
</LinearLayout>
<!-- 抽屉的内容部分 -->
<LinearLayout
android:background="@drawable/slidingdrawback"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal"
>
<ImageButton
android:id="@+id/upload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/upload"
android:onClick="click"
/>
<ImageButton
android:id="@+id/insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/insert"
android:onClick="click"
/>
<ImageButton
android:id="@+id/help"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/help"
android:onClick="click"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:gravity="center"
>
<ImageButton
android:id="@+id/bakup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/backup"
android:onClick="click"
/>
<ImageButton
android:id="@+id/about"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/about"
android:onClick="click"
/>
<ImageButton
android:id="@+id/exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/exit"
android:onClick="click"
/>
</LinearLayout>
</LinearLayout>
</SlidingDrawer>
</RelativeLayout>
界面代码块
package com.example.class3_;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;
import android.widget.Toast;
public class MainActivity extends Activity {
private SlidingDrawer mSd;
private ImageButton mIb;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mIb=(ImageButton) findViewById(R.id.ib);
mSd=(SlidingDrawer) findViewById(R.id.slidingDrawer);
//抽屉打开时候的监听器
mSd.setOnDrawerOpenListener(new OnDrawerOpenListener() {
public void onDrawerOpened() {
// 改变抽屉 样式图片
mIb.setBackgroundResource(R.drawable.close);
}
});
//抽屉关闭时候的监听器
mSd.setOnDrawerCloseListener(new OnDrawerCloseListener() {
public void onDrawerClosed() {
// 改变抽屉 样式图片
mIb.setBackgroundResource(R.drawable.open);
}
});
}
public void click(View view){
switch(view.getId()){
case R.id.insert:
Toast.makeText(this, "添加", 0).show();
break;
case R.id.help:
Toast.makeText(this, "帮助", 0).show();
break;
case R.id.upload:
Toast.makeText(this, "更新", 0).show();
break;
case R.id.bakup:
Toast.makeText(this, "后退", 0).show();
break;
case R.id.about:
Toast.makeText(this, "关于", 0).show();
break;
case R.id.exit:
Toast.makeText(this, "退出", 0).show();
break;
}
}
@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;
}
}