你們在開發中可能會遇到這樣的需求,實現一個側滑菜單,之前(long long ago)咱們都是用SlidingMenu實現的!那個時候處理策劃還基本上都是本身判斷滑動距離的,後來MaterialDesign的時候使用NavigationView和DrawerLayout就能很簡單的實現側滑的功能。閒話就說到這裏。。。java
其實若是你比較懶的話,在建立Activity的時候,你能夠不選擇那個空頁面的Activity,而像這樣選擇android
這樣就能夠直接建立一個相應的側滑頁面,不用去本身實現了。因爲本文主要是爲了講解關於怎麼去實現,因此這裏就本身去實現吧!bash
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimaryDark"
app:layout_constraintBottom_toTopOf="@id/dl_content"
app:layout_constraintTop_toTopOf="parent"
app:navigationIcon="@mipmap/back_icon"
app:title="導航菜單的展現"
app:titleTextColor="@android:color/white" />
<android.support.v4.widget.DrawerLayout
android:id="@+id/dl_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left"
app:headerLayout="@layout/nav_header"
app:menu="@menu/menu_navation_drawer" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="僞裝這裏有內容" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
複製代碼
簡單說明一下:由於一會要演示返回按鈕的問題,因此這裏的佈局是把DrawerLayout放在了Toolbar的下面,若是你直接建立的話,側滑出來的菜單會把Toolbar蓋住。app
別的就沒有什麼好說的了和其餘控件的屬性都差很少。ide
這個和寫平時的佈局都是同樣的。沒有什麼區別佈局
<?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="wrap_content"
android:gravity="center"
android:background="#198672"
android:minHeight="150dp"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher_round" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:textColor="@android:color/white"
android:layout_marginTop="10dp"
android:text="僞裝這裏有一個標題" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:textColor="@android:color/white"
android:text="僞裝這裏又有一個標題" />
</LinearLayout>
複製代碼
這裏須要有關menu的知識了?若是你不懂能夠看看我簡書的Android中menu的使用集錦,這裏就不講了。。。ui
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_camera"
android:icon="@mipmap/ic_call_white_24dp"
android:title="打電話" />
<item
android:id="@+id/nav_gallery"
android:icon="@mipmap/ic_directions_bike_white_24dp"
android:title="騎行" />
<item
android:id="@+id/nav_slideshow"
android:icon="@mipmap/ic_dialer_sip_white_24dp"
android:title="又是打電話" />
<item
android:id="@+id/nav_manage"
android:icon="@mipmap/ic_chat_bubble_outline_white_24dp"
android:title="消息" />
</group>
<item android:title="底部條目組">
<menu>
<item
android:id="@+id/nav_share"
android:icon="@mipmap/ic_call_missed_outgoing_white_24dp"
android:title="轉彎" />
<item
android:id="@+id/nav_send"
android:icon="@mipmap/ic_import_export_white_24dp"
android:title="好奇怪" />
</menu>
</item>
</menu>
複製代碼
而後咱們就能夠'Run' app了。。。就會看到以下驚悚的場面!!!this
佈局屬性spa
<style name="Theme.IconMenu">
<!--Menu/item attributes -->
<item name="android:itemTextAppearance">@android:style/TextAppearance.Widget.IconMenu.Item</item>
<item name="android:itemBackground">@android:drawable/menu_selector</item>
<item name="android:itemIconDisabledAlpha">?android:attr/disabledAlpha</item>
<item name="android:horizontalDivider">@android:drawable/divider_horizontal_dark</item>
<item name="android:verticalDivider">@android:drawable/divider_vertical_dark</item>
<item name="android:windowAnimationStyle">@android:style/Animation.OptionsPanel</item>
<item name="android:moreIcon">@android:drawable/ic_menu_more</item>
<item name="android:background">@null</item>
</style>
複製代碼
API3d
NavigationView nv_left = findViewById(R.id.nv_left);
nv_left.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_camera://條目的ID
Log.e(TAG, "onNavigationItemSelected: ");
break;
}
return true;
}
});
複製代碼
這裏有個問題,設置了監聽以後,條目會有點擊效果,而且不會在關閉相應的NavigationView。若是你返回true的話,條目會有相應的選中效果,注意一下就能夠了。
API
基本上的API就這麼多,若是感興趣的能夠研究如下DrawerView裏面提供的API。這裏就很少作解釋了。
效果大概是這個樣子的:
主要是看頂部左側的那個按鈕,主要是實現這個功能。這個功能主要是依託ActionBarDrawerToggle實現的,他聯動了DrawerLayout和Toolbar的關聯,看一下具體的代碼實現吧!
//這是帶Home旋轉開關按鈕
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, dl_content,
toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close);
dl_content.addDrawerListener(toggle);
toggle.syncState();
複製代碼
加上上面的代碼就能夠實現聯動了,可是你聯動的時候可能頂部的顏色是黑色的。反正我剛開始顯示的時候是黑色的。那麼怎麼把這個東西改爲白色呢?只有修改相應的主題樣式了。。。像這個樣子
<!-- 基類的主題添加中間那個內容 -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!--toolbar小漢堡樣式-->
<item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>
<!--設置左側按鈕的顏色-->
<style name="DrawerArrowStyle" parent="@style/Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">@android:color/white</item>
</style>
複製代碼
其實不使用上面的方法,能夠經過
isDrawerOpen(@EdgeGravity int drawerGravity)
的判斷處理返回按鈕的邏輯問題。
關於這個異常,網上的說法不少,有說必須是AppCompat的主題、有說必須有windowNoTitle屬性,都沒有解決這個異常,最後發現若是你在主題中設置了
<item name="android:textColorSecondary">#ffffff</item>
屬性的話,就會報這個異常。刪了就行了!
這裏處理起來挺奇葩的,你只要把NavigationView放在DrawerLayout的最後面就能夠了。監聽方法就會生效了。。。多是一個BUG吧(如此值汗顏)!
基本上關於兩個控件的組合就這麼多,若是有什麼問題歡迎留言。
歡迎關注,你的支持是我最大的動力