首先在MainActivity的XML文件中创建DrawerLayout和NavigationView,如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout"
tools:context=".MainActivity">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/drawer_example" />
</RelativeLayout>

<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="@+id/drawer_content"
app:menu="@menu/drawer_options"/>
</androidx.drawerlayout.widget.DrawerLayout>

我们可以看到NavigationView绑定了menu/drawer_options作为drawer中的选项

接下来,我们在res/文件夹中添加menu文件夹,并创建drawer_options.xml文件,并在其中添加两个switch,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:id="@+id/blur_switch"
android:title="Blur"
app:actionLayout="@layout/switch_item"
app:showAsAction="always" />

<item
android:id="@+id/light_switch"
android:title="Light"
app:actionLayout="@layout/switch_item"
app:showAsAction="always" />

</menu>

menu之中依旧是item,但是我们给其加上app:actionLayout=”switch layout 的 Destination”, switch layout的xml文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="@+id/drawer_switch_toggle"/>
</RelativeLayout>

现在我们将UI完成,运行一下看一下效果:

pic1

接下来我们需要给DrawerLayout添加上ActionBarDrawerToggle并且为Navigation Drawer中的swicth添加Listener做出反应。MainActivity.class代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.absolutelycold.drawerlayoutexample;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;

import android.content.res.Configuration;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;

import com.google.android.material.navigation.NavigationView;

public class MainActivity extends AppCompatActivity {

private NavigationView navigationView;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private Switch blurSwitch;
private Switch lightSwitch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


drawerLayout = findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_open, R.string.drawer_close);
drawerLayout.addDrawerListener(drawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);

navigationView = drawerLayout.findViewById(R.id.drawer_content);

// navigationView.getMenu() 之后得到了menu/drawer_menu渲染的layout
// findItem(id) 从menu layout中获取 item
// getActionView() 因为我在item设置了app:action_layout,所以得到的是switch的Layout
// findViewById() 得到的是switch layout中的switch 这个view
blurSwitch = navigationView.getMenu().findItem(R.id.blur_switch).getActionView().findViewById(R.id.drawer_switch);
lightSwitch = navigationView.getMenu().findItem(R.id.light_switch).getActionView().findViewById(R.id.drawer_switch);

//为两个switch设置listener
blurSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
Toast.makeText(MainActivity.this, "Blur switch checked", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(MainActivity.this, "Blur switch UnChecked", Toast.LENGTH_SHORT).show();
}
}
});

lightSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
Toast.makeText(MainActivity.this, "Light switch checked.", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(MainActivity.this, "Light switch UnChecked", Toast.LENGTH_SHORT).show();
}
}
});
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
//Do something when the home button is clicked
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
protected void onPostCreate(@Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}

@Override
public void onConfigurationChanged(@NonNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}

@Override
protected void onStart() {
super.onStart();
}
}

最终效果如下:

result