如何放置带有圆角的androidBottomAppBar

Jos*_*bre 10 xml android android-layout android-bottomappbar material-components-android

我正在使用BottomAppBar来自谷歌的这样的:

 <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@+id/vNavigationBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
Run Code Online (Sandbox Code Playgroud)

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

底部栏示例

我该怎么做才能以这种方式工作?

Gab*_*tti 8

BottomAppBar 与 a 一起使用MaterialShapeDrawable,您可以对其应用圆角(使用 a 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/..."
      ../>
Run Code Online (Sandbox Code Playgroud)

然后在代码中定义:

//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());
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

它也适用于fabCradle

<com.google.android.material.bottomappbar.BottomAppBar
    android:id="@+id/bottomAppBar"
    app:fabAlignmentMode="center"
    app:fabCradleVerticalOffset="8dp"
    app:fabCradleMargin="8dp"
    .../>
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

它需要版本1.1.0


小智 5

您可以尝试添加一个 shape drawable xml 文件并将以下代码添加到其中

<corners
    android:topLeftRadius="16dp"
    android:topRightRadius="16dp" />
Run Code Online (Sandbox Code Playgroud)

然后将BottomAppBar的背景设置为drawable