NavigationView的使用

  • 介紹
    一款專門做菜單的控件,一般和DrawerLayout一起使用。
  • 使用
    第一步,先準(zhǔn)備一個(gè)頭布局,一個(gè)菜單
先準(zhǔn)備一個(gè)表示頭部的布局。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
   android:layout_height="200dp">

   <ImageView
       android:clickable="true"
       android:id="@+id/iv"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:scaleType="centerCrop"
       android:background="#53fc77"/>//一個(gè)背景圖片
   <de.hdodenhof.circleimageview.CircleImageView
       android:clickable="true"
       android:id="@+id/it"
       android:layout_width="70dp"
       android:layout_height="73dp"
       android:src="@mipmap/xlj"
        android:layout_centerInParent="true"http://這里是采用了一個(gè)第三方的圓形頭像
       />
</RelativeLayout>
一般的布局形式,可以按照自己想要的去設(shè)計(jì),去布局。如上述布局,效果如下:
Paste_Image.png

在準(zhǔn)備下面的菜單文件:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    
    <item
        android:id="@+id/favorite"
        android:icon="@mipmap/ic_launcher"
        android:title="收藏"
        />
    <item
        android:id="@+id/wallet"
        android:icon="@mipmap/ic_launcher"
        android:title="錢包"/>

    <group android:id="@+id/g2">
        <item
            android:id="@+id/photo"
            android:icon="@mipmap/ic_launcher"
            android:title="相冊"/>
    </group>   //用group包裹,可以實(shí)現(xiàn)分割線,就是說,這個(gè)group中的內(nèi)容與上,與下面的內(nèi)容會出現(xiàn)分割線。
    <item
        android:id="@+id/file"
        android:icon="@mipmap/ic_launcher"
        android:title="文件"/>
</menu>

在布局文件中使用

 <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="left"   //這是和DrawerLayout配合使用時(shí)候帶的
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/header_layout"  //關(guān)鍵之一,引用頭部的布局
            app:menu="@menu/main">   //關(guān)鍵之二,引入菜單 
 </android.support.design.widget.NavigationView>
其中:android:fitsSystemWindows = "true":
內(nèi)置的一個(gè)布爾值屬性,通過其去調(diào)整基于系統(tǒng)窗口的視圖布局,例如狀態(tài)欄,如果為true,將自動(dòng)
調(diào)整系統(tǒng)窗口布局來適應(yīng)你自定義的布局。例如:當(dāng)系統(tǒng)有狀態(tài)欄,你的應(yīng)用也存在狀態(tài)欄時(shí)便可以設(shè)置為ture。
  • 添加點(diǎn)擊事件
 NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
  navigationView.setItemIconTintList(null); //這樣你在下面的菜單上的圖標(biāo)就會顯示原本的顏色。

 View headerview = navigationView.getHeaderView(0); //得到頭部的布局,
  heandImage = (ImageView) headerview.findViewById(R.id.iv);  //從頭布局中找到里面的控件,
可以添加點(diǎn)擊事件
circleImageView = (CircleImageView) headerview.findViewById(R.id.it);
針對與下面的菜單:
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    }
                return true;
            }
        });
在上面完成你所需要的點(diǎn)擊事件。

結(jié)果如下:

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

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

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