Toolbar + DrawerLayout
配置文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.bt" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity android:name=".MyActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
布局
Toolbar布局(tool_bar.xml)
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
android:popupTheme="@style/Theme.AppCompat"
>
</android.support.v7.widget.Toolbar>
DrawerLayout布局(drawer_layout.xml)
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
<ListView
android:id="@+id/list_view"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="20dp"
android:background="#f2cc1c4b">
</ListView>
</android.support.v4.widget.DrawerLayout>
主布局(main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/linear_layout">
<!-- Toolbar -->
<include layout="@layout/tool_bar"/>
<!-- DrawerLayout -->
<include layout="@layout/drawer_layout" />
</LinearLayout>
程序
package com.app.bt;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MyActivity extends AppCompatActivity{
private Toolbar mToolbar;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mActionBarDrawerToggle;
private ListView mListView;
private String[] str = {"item1","item2","item3","item4"};
private ArrayAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mToolbar = (Toolbar) findViewById(R.id.tool_bar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mListView = (ListView) findViewById(R.id.list_view);
mAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,str);
mToolbar.setTitle("Toolbar");
setSupportActionBar(mToolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mActionBarDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout,mToolbar,R.string.open,R.string.close){
@Override
public void onDrawerOpened(View drawerView) {
//Toast.makeText(MyActivity.this,"Open",Toast.LENGTH_SHORT).show();
super.onDrawerOpened(drawerView);
}
@Override
public void onDrawerClosed(View drawerView) {
//Toast.makeText(MyActivity.this,"Close",Toast.LENGTH_SHORT).show();
super.onDrawerClosed(drawerView);
}
};
mActionBarDrawerToggle.syncState();
mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
mListView.setAdapter(mAdapter);
}
}
参考文章:
【1】http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0303/2522.html
有问题的欢迎留言。