NavigationView


一、DrawerLayout


側(cè)面滑動導(dǎo)航欄。

  • 第一個子布局:內(nèi)容區(qū)。
  • 第二個子布局:導(dǎo)航欄布局
  • 注意導(dǎo)航欄布局里要設(shè)置 android:layout_gravity,"start" 表示左側(cè)。
<!-- A DrawerLayout is intended to be used as the top-level content view using 
match_parent for both width and height to consume the full space available. -->
<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">

    <!-- As the main content view, the view below consumes the entire 
    space available using match_parent in both dimensions. -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- android:layout_gravity="start" tells DrawerLayout to treat
    this as a sliding drawer on the left side for left-to-right
    languages and on the right side for right-to-left languages.
    The drawer is given a fixed width in dp and extends the full height of
    the container. A solid background is used for contrast
    with the content view. -->
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice">
    </android.support.v4.widget.DrawerLayout>

二、NavigationView


用來做 DrawerLayout 的導(dǎo)航欄布局

<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/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="250dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/navigation_header"
        app:menu="@menu/menu_navigation"/>

</android.support.v4.widget.DrawerLayout>
  • app:headerLayout:接收一個 layout,作為導(dǎo)航菜單頂部的 Header,可選項(xiàng)。
  • app:menu:接收一個 menu,作為導(dǎo)航菜單的菜單項(xiàng),幾乎是必選項(xiàng)。

menu:

<?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_item_1"
            android:icon="@drawable/nav_item_1"
            android:title="第一項(xiàng)" />
        <item
            android:id="@+id/nav_item_2"
            android:icon="@drawable/nav_item_2"
            android:title="第二項(xiàng)"/>
        <item
            android:title="第三項(xiàng)">
            <menu>
                <group
                    android:checkableBehavior="single">
                    <item
                        android:id="@+id/nav_item_3"
                        android:icon="@drawable/nav_item_1"
                        android:title="    第一小項(xiàng)"/>
                    <item
                        android:id="@+id/nav_item_4"
                        android:icon="@drawable/nav_item_2"
                        android:title="    第二小項(xiàng)"/>
                </group>
            </menu>
        </item>
        <item
            android:id="@+id/nav_item_7"
            android:icon="@drawable/nav_item_4"
            android:title="第四項(xiàng)"/>
        <item
            android:id="@+id/nav_item_8"
            android:title="第五項(xiàng)"
            android:icon="@drawable/nav_item_1" />
    </group>
</menu>
  • menu 里可嵌套子 menu。

menu item 點(diǎn)擊事件:

setNavigationItemSelectedListener() 方法

        navigationView=(NavigationView)findViewById(R.id.navigation_view);
        // 去掉scrollbar。scrollbar在NavigationView的child:NavigationMenuView中,
        navigationView.getChildAt(0).setVerticalScrollBarEnabled(false);
        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                        item.setChecked(true);
                        Toast.makeText(MainActivity.this, ""+item.getTitle(), Toast.LENGTH_SHORT).show();
                        return true;
                    }
                });

三、Toolbar 左上角圖標(biāo):


        drawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
        drawerToggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,
                R.string.drawer_open,R.string.drawer_close);
        drawerToggle.syncState();
        drawerLayout.addDrawerListener(drawerToggle);

打開關(guān)閉時圖標(biāo)有動畫變化。


四、導(dǎo)航欄與返回鍵:


似乎默認(rèn)按返回鍵會退出應(yīng)用而不是關(guān)閉導(dǎo)航欄,需要重寫下 onBackPressed()。

    // 按返回時若側(cè)邊導(dǎo)航欄是打開的,先退出
    @Override
    public void onBackPressed(){
        if(drawerLayout.isDrawerOpen(findViewById(R.id.navigation_view))){
            drawerLayout.closeDrawers();
        }else {
            super.onBackPressed();
        }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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