- 在 XML 布局文件中添加 BottomAppBar 和 FloatingActionButton。
- 在 Activity 或 Fragment 中设置 BottomAppBar 和 FloatingActionButton。
BottomAppBar bottomAppBar = findViewById(R.id.bottom_app_bar);
setSupportActionBar(bottomAppBar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// FAB 点击事件
}
});
- 自定义 FAB 的曲线位置。
public class CustomBottomAppBar extends BottomAppBar {
public CustomBottomAppBar(Context context) {
super(context);
init();
}
public CustomBottomAppBar(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomBottomAppBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
float fabRadius = getResources().getDimension(R.dimen.fab_size) / 2f;
float cradleVerticalOffset = getResources().getDimension(R.dimen.bottom_app_bar_cradle_vertical_offset);
float cradleWidth = getResources().getDimension(R.dimen.bottom_app_bar_cradle_width);
float fabVerticalOffset = getResources().getDimension(R.dimen.fab_vertical_offset);
CurveTopEdgeTreatment curveTopEdgeTreatment = new CurveTopEdgeTreatment(
fabRadius,
cradleVerticalOffset,
cradleWidth,
fabVerticalOffset
);
MaterialShapeDrawable materialShapeDrawable = getMaterialShapeDrawable();
if (materialShapeDrawable != null) {
materialShapeDrawable.setShapeAppearanceModel(
materialShapeDrawable.getShapeAppearanceModel()
.toBuilder()
.setTopEdge(curveTopEdgeTreatment)
.build()
);
}
}
}
- 在 XML 布局文件中使用自定义的 BottomAppBar。