android - 我们如何将 float 操作按钮放在任何布局的顶部

标签 android floating-action-button

我正在对 float 操作按钮进行一些操作。我有一个 ListView 并在屏幕底部显示谷歌广告, float 操作按钮显示在同一位置(底部|末端)。所以操作按钮隐藏了广告。

我想将 float 操作按钮移动到广告线性布局上方一点。

请看图片: enter image description here

我的代码是这样的:

<android.support.design.widget.FloatingActionButton
     android:id="@+id/fab"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_margin="@dimen/fab_margin"
     android:layout_alignParentBottom="true"
     android:layout_alignParentEnd="true"
     android:layout_alignParentRight="true"
     android:layout_gravity="bottom|end"
     android:src="@drawable/fav"
     android:adjustViewBounds="false" />


adLayoutId is ad layout id

请帮忙。

提前致谢。

最佳答案

您应该使用 RelativeLayout,正如我们所知,RelativeLayout 中靠后的子项往往会浮在 RelativeLayout 中靠前的子项之上。

因此,要让 FAB float 在任何布局之上,请确保在所有 View 之后(在 XML 布局的末尾)定义 FAB。

如果您使用 android:layout_margin="@dimen/fab_margin" 操作 FAB 的最佳方式是设置 View 之间的依赖关系,例如:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/adLayoutId"/>

    <LinearLayout
        android:id="@+id/adLayoutId"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:background="@android:color/black"/>

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/adLayoutId"
        android:layout_alignParentRight="true"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/fav"/>
</RelativeLayout>

float 操作按钮始终位于广告线性布局上方,并且独立于广告布局尺寸。 希望对您有所帮助。

关于android - 我们如何将 float 操作按钮放在任何布局的顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45019871/

相关文章:

android - 移动设备上的 SignalR JavaScript 客户端

android - 是否可以在 ARCore 运行时修改 3D 模型的结构?

android - 使用 ConnectivityManager 和 NetworkInfo 避免 NullPointerException

android - 带有新支持库的 Lollipop 之前的设备上的 FAB

android - 将 float 操作按钮添加到回收站 View 崩溃 : The style on this component requires your app theme to be Theme. AppCompat

Android 支持设计 float 操作按钮高度对于白色以外的颜色不可见

Android - 对话框 fragment 顶部的 float 操作按钮

java - 如何让 float 操作按钮在每次点击时旋转

具有固定后缀的 Android EditText

android - 如何使用数据绑定(bind)调用静态方法?