android - CardView 中心的 Fab 按钮

标签 android xml android-layout

我想为 CardView 创建一个类似于下图的布局,问题是我不知道将 FAB 放在布局中的何处,我尝试使用LinearLayout,但没有“重叠”CardView,或者是在里面或在外面,我需要一些建议

enter image description here

这是我尝试过的(仅是示例)

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/card"
    app:cardCornerRadius="4dp"
    app:cardElevation="5dp"
    app:cardUseCompatPadding="true">

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

       <!-- Other object -->

    </LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:clickable="true"
        android:src="@android:drawable/ic_dialog_info"
        app:backgroundTint="@android:color/holo_red_dark"
        app:elevation="6dp"
        app:borderWidth="0dp"
        app:layout_anchorGravity="center_horizontal"
        android:layout_gravity="center_horizontal" />

</android.support.v7.widget.CardView>

最佳答案

在这种情况下,FAB 需要位于 CardView 之外。将 CardView 包裹在 Frame 布局中。

<FrameLayout>
<android.support.v7.widget.CardView>
     // put content of card here
</android.support.v7.widget.CardView>
<Fab />
<FrameLayout>

关于android - CardView 中心的 Fab 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34254421/

相关文章:

android - MediaPlayer 在第一次运行时不工作

android - 如何使用 Google Drive API : Insert File 上传 FILE_URI

android - .apk 在 AVD 中重命名为 .txt

android - 尝试使用 NDK 为 Android 构建 PocketSphinx 时出现问题

java - SAXParseException;源代码解析 : Cannot resolve the name '...' to a(n) 'type definition' component

java - 滚动时图像滞后如何解决?

xml - 复制带有属性的 xml 文档

android - 在中心设置 ListView 的文本 - android

Android - RecyclerView StaggeredGridLayoutManager 最后一行的拉伸(stretch)宽度

android - 在布局中的子元素中使用非 android 命名空间前缀