androidx中简单使用侧滑菜单详解手机开发

布局文件

<?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/drawerLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity"> 
 
    <!-- 内容区 --> 
    <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="vertical"> 
 
        <Button 
            android:id="@+id/btn_open_left" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:onClick="open" 
            android:text="打开菜单" /> 
 
    </LinearLayout> 
 
    <!-- 左边菜单 --> 
    <com.google.android.material.navigation.NavigationView 
        android:id="@+id/navigation_view" 
        android:layout_width="200dp" 
        android:layout_height="match_parent" 
        android:layout_gravity="start" 
        app:headerLayout="@layout/drawer_header" 
        app:menu="@menu/menu_drawer_left" /> 
 
</androidx.drawerlayout.widget.DrawerLayout> 

com.google.android.material.navigation.NavigationView 需引入implementation ‘com.android.support:design:29.1.1’包

drawer_header.xml

<?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="120dp" 
    android:background="@color/colorPrimary" 
    android:gravity="center" 
    android:orientation="vertical"> 
 
    <ImageView 
        android:id="@+id/iv_head" 
        android:layout_width="80dp" 
        android:layout_height="80dp" 
        android:src="@mipmap/black" /> 
 
</LinearLayout> 

menu_drawer_left.xml

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
        android:id="@+id/all_item" 
        android:title="全部" /> 
    <item 
        android:id="@+id/a_item" 
        android:title="A" /> 
 
    <item 
        android:id="@+id/b_item" 
        android:title="B" /> 
</menu> 

Activity中处理

package com.hj.matchmaker; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.Toast; 
import androidx.annotation.NonNull; 
import androidx.appcompat.app.AppCompatActivity; 
import androidx.drawerlayout.widget.DrawerLayout; 
import com.google.android.material.navigation.NavigationView; 
public class MainActivity extends AppCompatActivity {
 
private DrawerLayout drawerLayout; 
private NavigationView navigation_view; 
private ImageView headView; 
@Override 
protected void onCreate(Bundle savedInstanceState) {
 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
drawerLayout = findViewById(R.id.drawerLayout); 
navigation_view = findViewById(R.id.navigation_view); 
headView = navigation_view.getHeaderView(0).findViewById(R.id.iv_head); 
headView.setOnClickListener(new View.OnClickListener() {
 
@Override 
public void onClick(View v) {
 
drawerLayout.closeDrawers(); 
Toast.makeText(MainActivity.this, "点击了图片", Toast.LENGTH_LONG).show(); 
} 
}); 
navigation_view.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
 
@Override 
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
 
switch (menuItem.getItemId()) {
 
case R.id.all_item: 
Toast.makeText(MainActivity.this, "点击了全部", Toast.LENGTH_LONG).show(); 
break; 
case R.id.a_item: 
Toast.makeText(MainActivity.this, "点击了a", Toast.LENGTH_LONG).show(); 
break; 
case R.id.b_item: 
Toast.makeText(MainActivity.this, "点击了b", Toast.LENGTH_LONG).show(); 
break; 
default: 
break; 
} 
drawerLayout.closeDrawers(); 
return true; 
} 
}); 
} 
public void open(View view) {
 
drawerLayout.openDrawer(Gravity.LEFT); 
} 
} 

效果图:
在这里插入图片描述

仅此记录

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/6212.html

(0)
上一篇 2021年7月17日
下一篇 2021年7月17日

相关推荐

发表回复

登录后才能评论