第一步的话当然就是我们的布局文件了:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/dl_main_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:background="@color/design_default_color_secondary"
android:gravity="center_vertical"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/main_menu"
android:src="@drawable/ic_baseline_apps_24"
android:background="@color/design_default_color_secondary"
android:layout_margin="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:text="智慧城市"
android:textColor="#ffffff"
android:textSize="20sp"
android:layout_weight="1"
android:layout_marginLeft="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/search"
android:src="@drawable/ic_baseline_apps_24"
android:background="@color/design_default_color_secondary"
android:layout_margin="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="主页"
android:textSize="22sp"
android:gravity="center"/>
</LinearLayout>
<!--然后的话我们的这个位置就是我们的navigation-->
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav"
android:layout_gravity="left"
android:fitsSystemWindows="true"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
app:headerLayout="@layout/navigation_header"
app:menu="@menu/drawer_view"
android:layout_height="match_parent"/>
</androidx.drawerlayout.widget.DrawerLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:background="?attr/colorPrimaryDark"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:layout_height="150dp">
<!--这个位置的话就是我们的啊头部的布局文件-->
<ImageView
android:layout_gravity="center_vertical"
android:layout_marginLeft="35dp"
android:background="@drawable/ic_baseline_ac_unit_24"
android:layout_width="100dp"
android:layout_height="100dp"/>
<TextView
android:text="智慧城市,赋能未来"
android:layout_gravity="center_vertical"
android:layout_marginLeft="15dp"
android:textSize="20dp"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
menu的视图文件:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!--这个文件的话就是我们的啊就是菜单文件-->
<group android:checkableBehavior="single">
<item
android:title="生活服务"
android:id="@+id/home"
android:icon="@drawable/ic_baseline_apps_24"
/>
<item
android:title="便民服务"
android:id="@+id/life_server"
android:icon="@drawable/ic_baseline_accessible_forward_24"
/>
<item
android:title="车主服务"
android:id="@+id/car_server"
android:icon="@drawable/ic_baseline_airport_shuttle_24"
/>
</group>
<!--于此同时我们的话还可以对我们的item添加我们的子菜单-->
<item android:title="其他服务">
<menu>
<item
android:title="设置"
android:id="@+id/seeting"
android:icon="@drawable/ic_baseline_accessible_forward_24"
/>
<item
android:title="关于我们"
android:id="@+id/myslf"
android:icon="@drawable/ic_baseline_airport_shuttle_24"
/>
</menu>
</item>
</menu>
然后的话就是我们的主要的java文件代码:
package com.example.navigationview;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.material.navigation.NavigationView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private DrawerLayout dl_main_drawer;
private NavigationView navigationView;
ImageView memu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initView();
//找到我们的就是id控件
dl_main_drawer = findViewById(R.id.dl_main_drawer);
navigationView = findViewById(R.id.nav);
memu = findViewById(R.id.main_menu);
// 获取我们的头部的布局
View headerview = navigationView.getHeaderView(0);
memu.setOnClickListener(this);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Toast.makeText(MainActivity.this,item.getTitle().toString(),Toast.LENGTH_LONG).show();
dl_main_drawer.closeDrawer(navigationView);
return true;
}
});
}
// 初始化我们的窗口属性,让状态栏和导航栏透明
private void initView() {
// if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
// tintManager = new SystemBarTintManager(this);
// int statusColor = Color.parseColor("#1976d2");
// tintManager.setStatusBarTintColor(statusColor);
// tintManager.setStatusBarTintEnabled(true);
//
// }
}
@Override
public void onClick(View view) {
// 设置我们的点击事件点检我们的菜单跳出我们的侧滑栏
switch (view.getId()){
case R.id.main_menu:
if(dl_main_drawer.isDrawerOpen(navigationView)){
dl_main_drawer.openDrawer(navigationView);
}else{
dl_main_drawer.openDrawer(navigationView);
}
break;
}
}
}