(1)效果展示
(2)实现过程
1.引入material库,并sync
//material库
implementation 'com.google.android.material:material:1.0.0'
2.准备好一张打钩的图
显示时的图片是这样的
3.修改activity_main.xml
<?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">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--自定义标题栏-->
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:background="#969C99"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<!-- 悬浮按钮-->
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="@drawable/yes"
app:elevation="8dp"
/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<!--滑动自定义菜单-->
<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"/>
</androidx.drawerlayout.widget.DrawerLayout>
4.修改MainActivity.java
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.snackbar.Snackbar;
/**
* 悬浮按钮和交互提示
*/
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar=findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mDrawerLayout=findViewById(R.id.drawerLayout);
NavigationView navigationView=findViewById(R.id.navView);
FloatingActionButton fab=findViewById(R.id.fab);//悬浮按钮
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//可交互提示
Snackbar.make(v,"删除",Snackbar.LENGTH_SHORT)
.setAction("取消", new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"已取消",Toast.LENGTH_SHORT).show();
}
}).show();
}
});
ActionBar actionBar=getSupportActionBar();
if (actionBar!=null){
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.menu);
}
// navigationView.setCheckedItem(R.id.navCall);//默认选中
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.navCall:
Toast.makeText(MainActivity.this,"电话",Toast.LENGTH_SHORT).show();
break;
case R.id.navFriends:
Toast.makeText(MainActivity.this,"朋友",Toast.LENGTH_SHORT).show();
break;
case R.id.navLocation:
Toast.makeText(MainActivity.this,"地址",Toast.LENGTH_SHORT).show();
break;
case R.id.nav_email:
Toast.makeText(MainActivity.this,"邮箱",Toast.LENGTH_SHORT).show();
break;
case R.id.navTask:
Toast.makeText(MainActivity.this,"任务",Toast.LENGTH_SHORT).show();
break;
}
mDrawerLayout.closeDrawers();
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.backup:
Toast.makeText(this,"备份",Toast.LENGTH_SHORT).show();
break;
case R.id.delete:
Toast.makeText(this,"删除",Toast.LENGTH_SHORT).show();
break;
case R.id.settings:
Toast.makeText(this,"设置",Toast.LENGTH_SHORT).show();
break;
case R.id.back:
Toast.makeText(this,"返回",Toast.LENGTH_SHORT).show();
break;
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START);
break;
}
return true;
}
}