【问题标题】:Card view with Fab Button like Material bottom App bar带有 Fab 按钮的卡片视图,例如 Material 底部应用栏
【发布时间】:2020-01-05 08:15:23
【问题描述】:

我可以使用 fab 按钮制作卡片视图。我可以在卡片视图中锚定 fab 按钮,但在这里我想制作一个看起来像 BottomAppBar(材料设计)的卡片视图。

代码

 <?xml version="1.0" encoding="utf-8"?>
    <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        tools:context=".fragments.ViewHistoryFragment"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        style="@style/Widget.MaterialComponents.BottomAppBar">

    <!--    style="@style/Widget.MaterialComponents.BottomAppBar"-->

        <com.google.android.material.card.MaterialCardView
            android:id="@+id/cv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/Widget.MaterialComponents.CardView">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:background="#C3EFC2">


                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="sdfkjgasdfjasdf"/>
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="sdfkjgasdfjasdf"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="sdfkjgasdfjasdf"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="sdfkjgasdfjasdf"/>


            </LinearLayout>

        </com.google.android.material.card.MaterialCardView>

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_anchor="@id/cv"
            android:src="@mipmap/ic_launcher"
            android:layout_marginEnd="@dimen/margin8"
           app:layout_anchorGravity="bottom|right|end"
            app:borderWidth="@dimen/margin8"
            app:backgroundTint="#343433" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

我想喜欢这种类型的 这意味着我想要我的卡片视图中的摇篮直径。 attached my wanted output

任何人的任何建议都将受到赞赏。

【问题讨论】:

  • 使用BottomAppBar
  • @GabrieleMariotti 也许如果我是对的,你不能使用 BottomApp Bar 代替卡片视图但是如果你认为可以,你可以给我一个例子。
  • 这种方式用过,不知道能不能用。在任何情况下,您都必须将 MaterialShapeDrawable 也应用于 CardView。检查此链接:medium.com/ackee/…
  • @GabrieleMariotti 你给我投反对票了吗?我不这么认为,但也许你的链接对我有用。
  • 绝对没有。这个问题很清楚也很有趣。

标签: android android-layout material-design android-cardview floating-action-button


【解决方案1】:

您可以申请MaterialCardViewEdgeTreatment

    BottomAppBarTopEdgeTreatment bottomAppBarTopEdgeTreatment = new BottomAppBarTopEdgeTreatment(
            fabMargin),
            roundedCornerRadius,
            cradleVerticalOffset
    );
    bottomAppBarTopEdgeTreatment.setFabDiameter(fabSize);

    MaterialCardView cardView = findViewById(R.id.card);
    cardView.setShapeAppearanceModel( cardView.getShapeAppearanceModel().toBuilder()
            .setTopEdge(bottomAppBarTopEdgeTreatment)
            .build());        

然后将Fab 锚定到CardView

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab"
    app:layout_anchorGravity="top|center"
    app:layout_anchor="@id/card"
    .../>

<com.google.android.material.card.MaterialCardView
    android:id="@+id/card"
    ../>

【讨论】:

  • fabMarginroundedCornerRadiuscradleVerticalOffsetsetFabDiameter中为默认FAB设置哪些值?
  • @NiceToMytyuk 他们应该是5dp8dp0dp56dp
猜你喜欢
  • 2019-03-11
  • 2019-03-12
  • 2019-11-08
  • 1970-01-01
  • 2020-08-22
  • 2020-11-03
  • 2019-04-24
  • 2018-12-08
  • 1970-01-01
相关资源
最近更新 更多