NavigationView简单简介和简单使用

本文介绍了Android中NavigationView的使用,它常与Toolbar和DrawerLayout配合实现侧滑菜单。通过引用'com.android.support:design:28.0.0'库,可以创建包含超级会员列表效果的NavigationView。在布局文件中声明并结合Menu来定制导航菜单。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

NavigationView

NavigationView

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

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

在这里插入图片描述

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);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<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>
<?xml version="1.0" encoding="utf-8"?>
<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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值