BottomAppBar与MaterialShapeDrawable
配合使用,您可以对其应用圆角(使用RoundedCornerTreatment
)。
在您的布局中:
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottom_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@color/..."
../>
然后在代码中定义:
//Corner radius
float radius = getResources().getDimension(R.dimen.default_corner_radius);
BottomAppBar bottomAppBar = findViewById(R.id.bottom_app_bar);
MaterialShapeDrawable bottomBarBackground = (MaterialShapeDrawable) bottomAppBar.getBackground();
bottomBarBackground.setShapeAppearanceModel(
bottomBarBackground.getShapeAppearanceModel()
.toBuilder()
.setTopRightCorner(CornerFamily.ROUNDED,radius)
.setTopLeftCorner(CornerFamily.ROUNDED,radius)
.build());
它也可以与fabCradle
一起使用:
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottomAppBar"
app:fabAlignmentMode="center"
app:fabCradleVerticalOffset="8dp"
app:fabCradleMargin="8dp"
.../>
需要版本1.1.0。
ViewCompat.setBackground(bottom_appbar, MaterialShapeDrawable(shapeAppearanceModel))`
- Akshay Raj.16f != 16dp
。 - Gabriele MariottiBottomAppBar
提供了一个MaterialShapeDrawable
。只需将圆角应用于它,改变ShapeAppearanceModel
即可。 - Gabriele MariottiAndroid L
,那么这就是正确的答案。 - Arshad Ali<corners
android:topLeftRadius="16dp"
android:topRightRadius="16dp" />
然后将BottomAppBar的背景设置为可绘制对象
`@Override protected void onLayout(boolean changed, int left, int top, int
right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mNavigationBarWidth = getWidth();
mNavigationBarHeight = getHeight();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPath.reset();
mPath = RoundedRect(0, 0, mNavigationBarWidth, mNavigationBarHeight, 50, 50, true);
canvas.drawPath(mPath, mPaint);
}
`
记住,在你自定义类的每个构造函数中,都要这样做:mPath = new Path();
mPaint = new Paint();
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setColor(Color.WHITE);
setBackgroundColor(Color.TRANSPARENT);