1.上图
![](https://i-blog.csdnimg.cn/blog_migrate/0bba41c3f09454aa5365235844f40b92.png)
![](https://i-blog.csdnimg.cn/blog_migrate/0a4891262d5ac25d1f65d7c77bafa94b.png)
- 资源引入
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
- 属性
属性 | 描述 |
---|---|
android:id | 控件id |
android:layout_width | 控件长度 |
android:layout_height | 控件高度 |
app:backgroundTint | 控件背景颜色 |
app:hideOnScroll | 滚动时隐藏 |
app:menu | 菜单选项 |
- 源代码
activity_bootom_app_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottomAppBar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_gravity="bottom"
app:backgroundTint="@color/black"
app:hideOnScroll="true"
app:menu="@menu/bab_menu" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/icon_scan"
app:fabSize="normal"
app:layout_anchor="@id/bottomAppBar" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
BootomAppBarActivity.java
package com.yyf.demo;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.yyf.demo.databinding.ActivityBottomAppBarBinding;
import java.util.ArrayList;
import java.util.List;
public class BottomAppBar extends AppCompatActivity {
private ActivityBottomAppBarBinding binding;
private static final String TAG = "BottomAppBar";
private List<String> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityBottomAppBarBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
for (int i = 0; i < 100; i++) {
list.add(String.valueOf(i));
}
binding.recyclerView.setLayoutManager(new LinearLayoutManager(getBaseContext()));
binding.recyclerView.setAdapter(new RecyclerView.Adapter() {
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_text, parent, false);
Holder holder = new Holder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
Holder holder1 = (Holder) holder;
TextView textView = holder1.itemView.findViewById(R.id.tv_item);
if (position % 2 == 0) {
textView.setBackgroundColor(ContextCompat.getColor(getBaseContext(), R.color.purple_500));
} else {
textView.setBackgroundColor(ContextCompat.getColor(getBaseContext(), R.color.purple_700));
}
textView.setText(list.get(position));
}
@Override
public int getItemCount() {
return list.size();
}
});
}
private static class Holder extends RecyclerView.ViewHolder {
public Holder(@NonNull View itemView) {
super(itemView);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
Log.d(TAG, "onOptionsItemSelected: " + item.getItemId());
switch (item.getItemId()) {
case R.id.scan:
Log.d(TAG, "onOptionsItemSelected: " + item.getTitle());
break;
}
return super.onOptionsItemSelected(item);
}
}
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/more"
android:icon="@drawable/ic_baseline_more_vert_24"
android:title="更多"
app:showAsAction="always">
<menu>
<item
android:id="@+id/scan"
android:title="扫一扫" />
</menu>
</item>
</menu>
- 注意事项
想要实现FloatingActionButton 嵌入BottomAppBar中得效果,必须使用CoordinatorLayout包裹,在FloatingActionButton中使用锚点app:layout_anchor=“引入BottomAppBar得控件Id”