这个类不在android开发工具包中,在android.support.v4.widget.DrawerLayout中,能实现左右侧滑的效果。
相关属性:
android:layout_gravity:设置抽屉的内容从左边或者右边出来
相关方法:
openDrawer(Gravity.LEFT | Gravity.RIGHT):设置左边 | 右边内容出来
closeDrawer(Gravity.LEFT | Gravity.RIGHT):关闭左边 | 右边内容出来
代码示意:
主框架代码:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/dl_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mixm.activity.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000" >
<include layout="@layout/activity_center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#00ff00" >
<include layout="@layout/activity_left" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="#0000ff" >
<include layout="@layout/activity_right" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
中间xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/btn_openLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开左边"
/>
<Button
android:id="@+id/btn_openRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="打开右边"
/>
</RelativeLayout>
左边屉子xml文件代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/btn_closeLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="关闭" />
</RelativeLayout>
右边屉子xml文件代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/btn_closeRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="关闭"
/>
</RelativeLayout>
java文件代码:
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button btn_closeLeft;
private Button btn_closeRight;
private Button btn_openLeft;
private Button btn_openRight;
private DrawerLayout dl_main;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
dl_main = (DrawerLayout) findViewById(R.id.dl_main);
btn_openLeft = (Button) findViewById(R.id.btn_openLeft);
btn_openRight = (Button) findViewById(R.id.btn_openRight);
btn_closeLeft = (Button) findViewById(R.id.btn_closeLeft);
btn_closeRight = (Button) findViewById(R.id.btn_closeRight);
// 添加打开监听事件
btn_openLeft.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dl_main.openDrawer(Gravity.LEFT);
}
});
btn_openRight.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dl_main.openDrawer(Gravity.RIGHT);
}
});
// 添加关闭监听事件
btn_closeLeft.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dl_main.closeDrawer(Gravity.LEFT);
}
});
btn_closeRight.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dl_main.closeDrawer(Gravity.RIGHT);
}
});
}
}