- 效果图
- 资源引入
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:checkedButton | 默认选中得按钮id |
app:selectionRequired | true 默认选择一个,不可不选,false 可不选 |
app:singleSelection | true 单选, false 多选 |
- 源代码
BottomAppBar.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.google.android.material.button.MaterialButtonToggleGroup;
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";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityBottomAppBarBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
binding.toggleButtonGroup.addOnButtonCheckedListener(new MaterialButtonToggleGroup.OnButtonCheckedListener() {
@Override
public void onButtonChecked(MaterialButtonToggleGroup group, int checkedId, boolean isChecked) {
switch (checkedId) {
case R.id.btn1:
Log.d(TAG, "onButtonChecked: 点击了btn1按钮 是否选中:" + isChecked);
break;
case R.id.btn2:
Log.d(TAG, "onButtonChecked: 点击了btn2按钮 是否选中:" + isChecked);
break;
case R.id.btn3:
Log.d(TAG, "onButtonChecked: 点击了btn3按钮 是否选中:" + isChecked);
break;
}
}
});
}
@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);
}
}
activity_bottom_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">
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/toggleButtonGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
app:checkedButton="@id/btn1"
app:selectionRequired="true"
app:singleSelection="false">
<com.google.android.material.button.MaterialButton
android:id="@+id/btn1"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="aaa" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btn2"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bbb" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btn3"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ccc" />
</com.google.android.material.button.MaterialButtonToggleGroup>
<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>