7
votes

Comment mettre l'Android BottomAppBar avec des coins arrondis

J'utilise le BottomAppBar de Google comme ceci:

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

La barre inférieure personnalisée est plate et je dois ajouter des coins arrondis sur la barre inférieure (image exemple ci-dessous)

 Exemple de barre inférieure

Que dois-je faire pour que cela fonctionne de cette façon?


1 commentaires

Je recommande fortement de ne pas faire ce que vous avez en tête, car cela va à l'encontre des directives de conception matérielle: material.io/design/components/app-bars-bottom.html#


3 Réponses :


5
votes

vous pouvez essayer d'ajouter un fichier xml de forme dessinable et y ajouter le code suivant

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

Et puis définir l'arrière-plan de BottomAppBar sur le dessinable


0 commentaires

1
votes

selon ceci , vous pouvez créer votre classe customView étendue à partir de BottomAppBar et implémenter ce code:

mPath = new Path();
    mPaint = new Paint();
    mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    mPaint.setColor(Color.WHITE);
    setBackgroundColor(Color.TRANSPARENT);

`

rappelez-vous juste dans chaque constructeur de votre classe personnalisée, faites ceci:

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


0 commentaires

5
votes

Le BottomAppBar fonctionne avec un MaterialShapeDrawable et vous pouvez lui appliquer des coins arrondis (en utilisant un RoundedCornerTreatment ).

Dans votre mise en page :

<com.google.android.material.bottomappbar.BottomAppBar
    android:id="@+id/bottomAppBar"
    app:fabAlignmentMode="center"
    app:fabCradleVerticalOffset="8dp"
    app:fabCradleMargin="8dp"
    .../>

Ensuite dans le code définissez:

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

 entrez la description de l'image ici

Cela fonctionne aussi avec un fabCradle:

  <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/..."
      ../>

 entrez la description de l'image ici p >

Il nécessite la version 1.1.0


4 commentaires

ne fonctionne pas. code>


@AkshayRaj. Votre code fonctionne , juste essayé. Mais 16f! = 16dp .


@AkshayRaj. J'ai mis à jour la réponse. Le BottomAppBar fournit un MaterialShapeDrawable . Il suffit de lui appliquer les coins arrondis en changeant le ShapeAppearanceModel


Cette réponse est correcte si vous ciblez Android L