如何将Android BottomAppBar带圆角

时间:2019-01-28 16:34:51

标签: android xml android-layout android-bottomappbar

我正在这样使用Google的BottomAppBar:

 <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@+id/vNavigationBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

自定义底部栏是平坦的,我需要在底部栏上添加圆角(图像示例如下)

Bottom bar example

我应该怎么做才能做到这一点?

3 个答案:

答案 0 :(得分:0)

您可以尝试添加形状可绘制的xml文件,并向其中添加以下代码

<corners
    android:topLeftRadius="16dp"
    android:topRightRadius="16dp" />

然后将BottomAppBar的背景设置为drawable

答案 1 :(得分:0)

根据this,您可以创建从BottomAppBar扩展的customView类,并实现以下代码:

`@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);

答案 2 :(得分:0)

您可以使用材料组件库提供的 MaterialShapeDrawable

在您的布局中:

  <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);

ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel()
    .toBuilder()
    .setTopRightCorner(CornerFamily.ROUNDED,radius)
    .setTopLeftCorner(CornerFamily.ROUNDED,radius)
    .build();

ViewCompat.setBackground(bottomAppBar,new MaterialShapeDrawable(shapeAppearanceModel));

enter image description here

它需要版本 1.1.0