NavigationView是Material库中提供的一个控件,可以将滑动菜单页面变得非常简单。
- 添加依赖
文件位置:
dependencies {
//其他内容省略...
//Material库
implementation 'com.google.android.material:material:1.1.0'
//一个开源项目 CircleImageView,可以用来实现图片圆形化的功能
implementation 'de.hdodenhof:circleimageview:3.0.1'
}
- 更换主题
文件位置:
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="Theme.NetWorkTest" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
- 新建文件夹menu->新建nav_menu.xml
文件位置: 编写NavigationView的菜单项 (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/note_group"
android:icon="@drawable/group"
android:title="@string/note_group_nav" />
<item
android:id="@+id/note_collect"
android:icon="@drawable/collect"
android:title="@string/note_collect_nav"
/>
<item
android:id="@+id/note_setting"
android:icon="@drawable/setting"
android:title="@string/note_setting_nav"
/>
</group>
</menu>
- 编写nav_header.xml (headerlayout)
文件位置:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:padding="10dp"
android:background="@color/teal_700"
android:layout_height="180dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/iconImage"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/nav_icon"
android:layout_centerInParent="true"
>
</de.hdodenhof.circleimageview.CircleImageView>
<TextView
android:id="@+id/user_mail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="@string/user_mail"
android:textColor="@color/white"
android:textSize="14sp"
>
</TextView>
<TextView
android:id="@+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/user_name"
android:layout_above="@id/user_mail"
android:textColor="@color/white"
android:textSize="14sp"
>
</TextView>
</RelativeLayout>
- 修改activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/drawer_layout"
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=".MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<androidx.appcompat.widget.Toolbar
android:id="@+id/tool_bar"
xmlns:app="http //schemas android com/apk/res-auto"
app:navigationIcon="@drawable/navi"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/purple_200"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
></androidx.appcompat.widget.Toolbar>
</FrameLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/nav_menu"
app:headerLayout="@layout/nav_header"
>
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
- 在MainActivity.java中处理菜单项的点击事件
package com.example.networktest;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import com.google.android.material.navigation.NavigationView;
public class MainActivity extends AppCompatActivity {
private Toolbar tool_bar;
private DrawerLayout drawerLayout;
private NavigationView navView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
listen();
}
private void init() {
navView = findViewById(R.id.navView);
drawerLayout = findViewById(R.id.drawer_layout);
tool_bar = findViewById(R.id.tool_bar);
setSupportActionBar(tool_bar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.navi);
navView.setCheckedItem(R.id.note_group);
}
private void listen() {
navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.note_group:
Toast.makeText(getApplication(), "你点击了note_group", Toast.LENGTH_SHORT).show();
break;
case R.id.note_collect:
Toast.makeText(getApplication(), "你点击了note_collect", Toast.LENGTH_SHORT).show();
break;
case R.id.note_setting:
Toast.makeText(getApplication(), "你点击了note_setting", Toast.LENGTH_SHORT).show();
break;
}
drawerLayout.closeDrawers();
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
drawerLayout.openDrawer(GravityCompat.START);
break;
case R.id.love:
Toast.makeText(this, "you clicked love", Toast.LENGTH_SHORT).show();
break;
case R.id.save:
Toast.makeText(this, "you clicked save", Toast.LENGTH_SHORT).show();
break;
case R.id.rew:
Toast.makeText(this, "you clicked rewrite", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
}
运行效果图:
|