NavigationView简单简介和简单使用

NavigationView

    • NavigationView

NavigationView

NavigationView和Toolbar+DrawerLayout连用 详情链接
https://blog.csdn.net/SageDeceiveFiend/article/details/91357584
实现我的超级会员列表的效果

需要导依赖 implementation ‘com.android.support:design:28.0.0’
是列表组件
需要在布局文件声明

NavigationView简单简介和简单使用_第1张图片

package com.example.sixeightwork.Day03;

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.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sixeightwork.R;

public class ToolBarTestActivity extends AppCompatActivity {

    NavigationView navigationView;
    Toolbar toolbar;
    DrawerLayout drawerLayout;
    TextView textView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tool_bar_test);

        initview();

        initDarerLayout();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

       getMenuInflater().inflate(R.menu.toolbar_menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {


        return super.onOptionsItemSelected(item);
    }

    public void initview(){
        navigationView=findViewById(R.id.nv);

        textView=findViewById(R.id.textv);
        toolbar=findViewById(R.id.tb);
        drawerLayout=findViewById(R.id.dl);
        setSupportActionBar(toolbar);//让他替换actionbar
        //导航按钮添加点击事件
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(drawerLayout.isDrawerOpen(Gravity.LEFT)){
                    drawerLayout.closeDrawer(Gravity.LEFT);
                }else{
                    drawerLayout.openDrawer(Gravity.LEFT);
                }
            }
        });
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(ToolBarTestActivity.this, "点击了导航按钮", Toast.LENGTH_SHORT).show();
            }
        });



    }

    public void initDarerLayout(){



        //绑定toolbar和drawerLayout
        ActionBarDrawerToggle toggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.openlayout,R.string.closelayout);

        toggle.syncState();
        drawerLayout.addDrawerListener(toggle);
    }
}


<RelativeLayout 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=".Day03.ToolBarTestActivity">

    <android.support.v7.widget.Toolbar
        app:navigationIcon="@drawable/ic_launcher_background"
        app:subtitleTextColor="#4f5"
        app:subtitle="副标题"
        app:titleTextColor="#f40"
        app:title="标题"
        app:logo="@mipmap/ic_launcher_round"
        android:background="#f95"
        android:id="@+id/tb"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <EditText
                android:layout_width="100dp"
                android:layout_height="wrap_content" />
        LinearLayout>

    android.support.v7.widget.Toolbar>

    <android.support.v4.widget.DrawerLayout
        android:layout_below="@id/tb"

        android:id="@+id/dl"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:background="#65f"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/textv"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="这是主界面" />
        LinearLayout>

        <android.support.design.widget.NavigationView

            android:id="@+id/nv"
            app:menu="@menu/newmenu"
            android:layout_gravity="left"
            app:headerLayout="@layout/heard"
            android:layout_width="match_parent"
            android:layout_height="match_parent">android.support.design.widget.NavigationView>
    android.support.v4.widget.DrawerLayout>
RelativeLayout>

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/men1" android:title="我的超级会员" android:icon="@drawable/ic_launcher_background"></item>
        <item android:id="@+id/men2" android:title="我的腾讯会员" android:icon="@drawable/ic_launcher_background"></item>
        <item android:id="@+id/men3" android:title="我的音乐绿钻" android:icon="@drawable/ic_launcher_background"></item>
        <item android:id="@+id/men4" android:title="我的空间黄钻" android:icon="@drawable/ic_launcher_background"></item>
        <item android:id="@+id/men5" android:title="我的地下黑钻" android:icon="@drawable/ic_launcher_background"></item>


    </group>

</menu>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <ImageView

        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="@drawable/ic_launcher_background"
        />
RelativeLayout>

你可能感兴趣的:(简单控件方法的使用与了解)