Android使用NavigationView和Toolbar和DrawLayout實(shí)現(xiàn)側(cè)滑菜單

1、首先需要添加appcompat-v7支持:

如果是在Android Studio 2.1 Preview3 上創(chuàng)建的項(xiàng)目,默認(rèn)已經(jīng)添加了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">

? ? <!-- 你的主界面內(nèi)容,必須放置在DrawerLayout中的第一個(gè)位置-->

? ? <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實(shí)現(xiàn)的核心代碼:

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);

? ? ? ? // 主標(biāo)題

? ? ? ? toolbar.setTitle("Title");

? ? ? ? // 副標(biāo)題

//? ? ? ? toolbar.setSubtitle("Sub Title");

? ? ? ? //設(shè)置toolbar

? ? ? ? setSupportActionBar(toolbar);

? ? ? ? //左上角圖標(biāo)可用

? ? ? ? getSupportActionBar().setDisplayHomeAsUpEnabled(true);

? ? ? ? getSupportActionBar().setHomeButtonEnabled(true);

? ? ? ? //左邊的小箭頭(注意需要在setSupportActionBar(toolbar)之后才有效果)

//? ? ? ? toolbar.setNavigationIcon(R.mipmap.iv_icon_douban);

? ? ? ? //菜單點(diǎn)擊事件(注意需要在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();

? ? ? ? //設(shè)置返回鍵動(dòng)畫(huà)

? ? ? ? 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;

? ? }

}

---------------------

作者:別等時(shí)光染了夢(mèng)想

來(lái)源:CSDN

原文:https://blog.csdn.net/wangxueqing52/article/details/80265024

版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請(qǐng)附上博文鏈接!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容