1、首先需要添加appcompat-v7支持:
如果是在Android Studio 2.1 Preview3 上创建的项目,默认已经添加了appcompat-v7和design支持了,如果不是最新版AndroidStudio则需要在build.gradle中添加如下代码:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.2.0'
compile 'com.android.support:design:23.2.0'
}
然后Ctrl+F9编译。
2、在主布局文件添加DrawerLayout:
activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 你的主界面内容,必须放置在DrawerLayout中的第一个位置--> <FrameLayout android:id="@+id/ly_content" android:layout_width="match_parent" android:layout_height="match_parent" /> <include layout="@layout/toolbar"/> <android.support.design.widget.NavigationView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:id="@+id/navigation_view" app:menu="@menu/menu_content" app:headerLayout="@layout/popup" android:fitsSystemWindows="true"> </android.support.design.widget.NavigationView> </android.support.v4.widget.DrawerLayout>
3、添加包含ToolBar:
在AndroidManifest.xml需要去掉自带的ActionBar:
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" <!-- 去掉ActionBar --> android:theme="@style/Theme.AppCompat.Light.NoActionBar"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
定义toolbar.xml的样式
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.Toolbar android:background="@color/colorPrimaryDark" android:id= "@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:popupTheme="@style/ThemeOverlay.AppCompat.Light"> </android.support.v7.widget.Toolbar> </RelativeLayout>
4、Java实现的核心代码:
package supermap.com.myapplication; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.design.widget.NavigationView; import android.support.v4.app.FragmentManager; 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.Menu; import android.view.MenuItem; import android.view.View; import android.widget.FrameLayout; import butterknife.BindView; import butterknife.ButterKnife; public class Main7Activity extends AppCompatActivity { @BindView(R.id.ly_content) FrameLayout lyContent; @BindView(R.id.drawer_layout) DrawerLayout drawerLayout; @BindView(R.id.toolbar) Toolbar toolbar; @BindView(R.id.navigation_view) NavigationView navigationView; ActionBarDrawerToggle mDrawerToggle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main7); ButterKnife.bind(this); // Logo toolbar.setLogo(R.mipmap.iv_icon_baidu); // 主标题 toolbar.setTitle("Title"); // 副标题 // toolbar.setSubtitle("Sub Title"); //设置toolbar setSupportActionBar(toolbar); //左上角图标可用 getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); //左边的小箭头(注意需要在setSupportActionBar(toolbar)之后才有效果) // toolbar.setNavigationIcon(R.mipmap.iv_icon_douban); //菜单点击事件(注意需要在setSupportActionBar(toolbar)之后才有效果) // toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { // @Override // public boolean onMenuItemClick(MenuItem item) { // Log.e("b", false + ""); // return true; // } // }); mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name) { @Override public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); } @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); } }; mDrawerToggle.syncState(); //设置返回键动画 drawerLayout.setDrawerListener(mDrawerToggle); navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { BlankFragment contentFragment = new BlankFragment(); Bundle args = new Bundle(); args.putString("text", "text"); contentFragment.setArguments(args); FragmentManager fm = getSupportFragmentManager(); fm.beginTransaction().replace(R.id.ly_content, contentFragment).commit(); drawerLayout.closeDrawer(navigationView); return true; } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // getMenuInflater().inflate(R.menu.menu_item, menu); return true; } }