NavigationView

NavigationView

    • 一、效果图
    • 二、代码示例

一、效果图

NavigationView_第1张图片

二、代码示例

//头部局布局文件
<?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:orientation="vertical">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_launcher_background"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="这是头布局"/>

</LinearLayout>
//Activity布局文件
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bw.day03.Main3Activity"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#00ffff"
        app:title="标题"
        app:subtitle="副标题"
        app:navigationIcon="@mipmap/ic_launcher">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="请输入你要搜索的内容"/>
        </LinearLayout>
    </android.support.v7.widget.Toolbar>

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="这是主页面"
                android:textSize="30sp"
                android:gravity="center"/>
        </LinearLayout>

        <android.support.design.widget.NavigationView
            android:id="@+id/navigationView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            app:headerLayout="@layout/header"//设置头部局
            app:menu="@menu/navigation_item">//设置menu布局

        </android.support.design.widget.NavigationView>

    </android.support.v4.widget.DrawerLayout>
</LinearLayout>

//java代码
package com.bw.day03;

import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

import day01.bw.com.myapplication.R;

public class Main3Activity extends AppCompatActivity {

    Toolbar toolbar;
    DrawerLayout drawerLayout;
    ActionBarDrawerToggle toggle;
    NavigationView navigationView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //把原本自带的标题栏去掉 (ps:可以在xml文件里去 也可以用代码去掉)
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main3);
        navigationView = findViewById(R.id.navigationView);
        toolbar = findViewById(R.id.toolBar);
        drawerLayout = findViewById(R.id.drawerLayout);
        setSupportActionBar(toolbar);



        //导航按钮点击事件
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(Main3Activity.this, "导航按钮点击事件", Toast.LENGTH_SHORT).show();
            }
        });

        //对ToolBar和DrawerLayout的绑定
        toggle = new ActionBarDrawerToggle(Main3Activity.this,drawerLayout,toolbar,R.string.open_layout,R.string.close_layout);
        toggle.syncState();
        drawerLayout.addDrawerListener(toggle);
        //NavigationLayout监听事件
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                return false;
            }
        });

    }

    //创建系统菜单 显示在标题栏的右边
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar_item,menu);
        return true;
    }

    //系统菜单监听事件
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.menu1:
                Log.e("###菜单","菜单一");
                break;
            case R.id.childmenu1:
                Log.e("###菜单","子菜单一");
                //点击子菜单显示抽屉和关闭抽屉
                if(drawerLayout.isDrawerOpen(Gravity.LEFT)){
                    drawerLayout.closeDrawer(Gravity.LEFT);
                }else{
                    drawerLayout.openDrawer(Gravity.LEFT);
                }
                break;
            case R.id.childmenu2:
                Log.e("###菜单","子菜单二");
                break;
            case R.id.childmenu3:
                Log.e("###菜单","子菜单三");
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}

//navigationView menu文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group android:checkableBehavior="none">
        <item android:id="@+id/supervip" android:icon="@mipmap/ic_launcher_round" android:title="我的超级会员"></item>
        <item android:id="@+id/money" android:icon="@mipmap/ic_launcher_round" android:title="QQ钱包"></item>
        <item android:id="@+id/decoration" android:icon="@mipmap/ic_launcher_round" android:title="个人装扮"></item>
        <item android:id="@+id/mylove" android:icon="@mipmap/ic_launcher_round" android:title="我的收藏"></item>
    </group>
</menu>

你可能感兴趣的:(Android_Three)