简体   繁体   English

如何使用自定义形状的 fab 按钮制作底部栏?

[英]How make Bottombar with custom shape fab button?

I want to make a bottombar with attach fab button like given below image.我想制作一个带有附加工厂按钮的底部栏,如下图所示。 If anyone knows about that type of different shape button library with a bottom with fab then suggest to me.如果有人知道那种底部带有晶圆厂的不同形状的按钮库,那么请向我推荐。

The image is given below make a bottombar with fab like this.下面给出的图像用这样的工厂制作一个底栏。

在此处输入图像描述

It is just an idea the code can be improved.这只是一个可以改进代码的想法。
You can change the shape of the FloatingActionButton with the shapeAppearanceOverlay attribute:您可以使用shapeAppearanceOverlay属性更改FloatingActionButton的形状:

<com.google.android.material.floatingactionbutton.FloatingActionButton
    app:shapeAppearanceOverlay="@style/cutfab"
    ..>

with:和:

<style name="cutfab">
    <item name="cornerFamily">cut</item>
    <item name="cornerSize">15dp</item>
</style>

Then you can define the BottomAppBar in your layout:然后您可以在布局中定义BottomAppBar

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

Finally you can apply to the BottomAppBar a TopEdgeTreatment .最后,您可以向BottomAppBar应用TopEdgeTreatment Something like:就像是:

BottomAppBar bar = findViewById(R.id.bar);
BottomAppBarTopEdgeTreatment topEdge = new BottomAppBarCutCornersTopEdge(
        bar.getFabCradleMargin(),
        bar.getFabCradleRoundedCornerRadius(),
        bar.getCradleVerticalOffset());
MaterialShapeDrawable babBackground = (MaterialShapeDrawable) bar.getBackground();

babBackground.setShapeAppearanceModel(
  babBackground.getShapeAppearanceModel()
  .toBuilder()
  .setTopEdge(topEdge)
  .build());

在此处输入图像描述

Where the BottomAppBarCutCornersTopEdge is something like: BottomAppBarCutCornersTopEdge类似于:

public class BottomAppBarCutCornersTopEdge extends BottomAppBarTopEdgeTreatment {

    private final float fabMargin;
    private final float cradleVerticalOffset;

    BottomAppBarCutCornersTopEdge(
            float fabMargin, float roundedCornerRadius, float cradleVerticalOffset) {
        super(fabMargin, roundedCornerRadius, cradleVerticalOffset);
        this.fabMargin = fabMargin;
        this.cradleVerticalOffset = cradleVerticalOffset;
    }

    @Override
    @SuppressWarnings("RestrictTo")
    public void getEdgePath(float length, float center, float interpolation, ShapePath shapePath) {
        float fabDiameter = getFabDiameter();
        if (fabDiameter == 0) {
            shapePath.lineTo(length, 0);
            return;
        }

        float diamondSize = fabDiameter / 2f;
        float middle = center + getHorizontalOffset();

        float verticalOffsetRatio = cradleVerticalOffset / diamondSize;
        if (verticalOffsetRatio >= 1.0f) {
            shapePath.lineTo(length, 0);
            return;
        }

        shapePath.lineTo(middle - (fabMargin + diamondSize), 0);    
        shapePath.lineTo(middle - fabDiameter/3, (diamondSize - cradleVerticalOffset + fabMargin) * interpolation);    
        shapePath.lineTo(middle + fabDiameter/3, (diamondSize - cradleVerticalOffset + fabMargin) * interpolation);    
        shapePath.lineTo(middle + (fabMargin + diamondSize), 0);    
        shapePath.lineTo(length, 0);
    }

}

To obtain a better result you should extend the CutCornerTreatment , implementing in the getCornerPath method the same path used in the BottomAppBar and apply it to the FloatingActionButton .为了获得更好的结果,您应该扩展CutCornerTreatment ,在getCornerPath方法中实现与BottomAppBar中使用的相同路径并将其应用于FloatingActionButton

In order to use latest style BottomAppBar in your app.为了在您的应用程序中使用最新样式的 BottomAppBar。

1) Include Google Maven repository in build.gradle. 1)在 build.gradle 中包含 Google Maven 存储库。

allprojects {
repositories {
  jcenter()
  maven {
    url "https://maven.google.com"
   }
  }
}

2) Place material components dependency in your build.gradle . 2)将材料组件依赖项放在您的build.gradle中。 Keep in mind that material version is regularly updating.请记住,材料版本会定期更新。

implementation 'com.google.android.material:material:1.0.0-alpha1'

3) Make sure your app inherits Theme.MaterialComponents theme in order to make BottomAppBar use the latest style. 3)确保您的应用继承Theme.MaterialComponents主题,以使 BottomAppBar 使用最新样式。 Alternatively you can declare the style for BottomAppBar in widget declaration within layout xml file as follows.或者,您可以在布局 xml 文件中的小部件声明中声明 BottomAppBar 的样式,如下所示。

style=”@style/Widget.MaterialComponents.BottomAppBar”

You can include BottomAppBar in your layout as follows.您可以在布局中包含 BottomAppBar,如下所示。 BottomAppBar must be a child of CoordinatorLayout. BottomAppBar 必须是 CoordinatorLayout 的子级。

<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottom_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:backgroundTint="@color/colorPrimary"
app:fabAlignmentMode="center"
app:fabAttached="true"
app:navigationIcon="@drawable/baseline_menu_white_24"/>

You can anchor a Floating Action Button (FAB) to BottomAppBar by specifying the id of the BottomAppBar in app:layout_anchor attribute of the FAB.您可以通过在 FAB 的app:layout_anchor属性中指定 BottomAppBar 的 id 将浮动操作按钮 (FAB) 锚定到 BottomAppBar。 BottomAppBar can cradle FAB with a shaped background or FAB can overlap BottomAppBar. BottomAppBar 可以将 FAB 放置在具有形状背景的支架上,或者 FAB 可以与 BottomAppBar 重叠。

<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/baseline_add_white_24"
app:layout_anchor="@id/bottom_app_bar" />

THANKS谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM