相关介绍
图:

有关的类:
- ActionBar: V7
- DrawerLayout:v4
- ActionBarDrawerToggle:v4
步骤:
- 获取ActionBar + DrawerLayout控件
- 设置ActionBar
- 创建ActionBarDrawerToggle对象,并同步
- 给添加DrawerLayout监听
代码:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawerLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.cqc.drawerlayoutdemo1.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="内容页"
android:textSize="25sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#ffffff">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="侧拉页"
android:textSize="25sp"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
MainActivity.java
package com.cqc.drawerlayoutdemo1;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle toggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initActinBar();
}
private void initView() {
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
}
/**
* 有关的类:ActionBar + ActionBarDrawerToggle + DrawerLayout
*/
private void initActinBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setLogo(R.mipmap.ic_launcher);
actionBar.setTitle(getString(R.string.app_name));
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
toggle = new ActionBarDrawerToggle(this,drawerLayout,R.drawable.ic_drawer_am,0,0);
toggle.syncState();
drawerLayout.setDrawerListener(toggle);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
toggle.onOptionsItemSelected(item);
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}