我们如何将浮动操作按钮放在任何布局之上

问题描述 投票:0回答:6

我正在对浮动操作按钮进行一些操作。我有一个列表视图,并在屏幕底部显示谷歌广告,浮动操作按钮显示在同一位置(底部|末尾)。所以操作按钮隐藏了广告。

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

请看一下图片:

我的代码如下:

<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

请帮忙。

提前致谢。

android floating-action-button
6个回答
8
投票

您应该使用

RelativeLayout
,正如我们所知,
RelativeLayout
中较晚的孩子往往会漂浮在
RelativeLayout
中较早的孩子上。

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

如果使用

android:layout_margin="@dimen/fab_margin"
,操作FAB的最佳方法是设置视图之间的依赖关系,例如:

<?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>

浮动操作按钮始终位于广告线性布局上方,并且与广告布局尺寸无关。 希望有帮助。


1
投票
<android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_marginBottom="100dp"
        android:layout_marginRight="30dp"
       />

1
投票

fab 按钮内的这两行帮助我解决了这个问题:

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"

这是我单独用于按钮的完整代码:

 <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab_newStudent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        android:contentDescription="@string/todo"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@drawable/ic_baseline_add_24" />

0
投票

这两行很重要。

android:layout_alignParentEnd =“true”

android:layout_alignParentBottom =“true”


0
投票
<com.google.android.material.bottomappbar.BottomAppBar
    android:id="@+id/bottomAppBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    app:fabCradleMargin="10dp"
    android:backgroundTint="@color/bg_bottomnav"
    app:fabCradleRoundedCornerRadius="10dp"
    app:fabCradleVerticalOffset="10dp">

    <!-- Bottom Navigation View -->
    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="bottom"
        android:background="@android:color/transparent"
        app:menu="@menu/bottom_menu"
        app:itemIconTint="@color/bottomicon_color"
        app:tint="@color/white"
        android:backgroundTint="@color/white"/>

</com.google.android.material.bottomappbar.BottomAppBar>

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/floatingActionButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@string/app_name"
    android:layout_marginBottom="60dp"
    android:src="@drawable/baseline_add_circle_24"
    app:layout_anchor="@id/bottomAppBar"
    app:tint="@color/white"
    android:backgroundTint="@color/bottomicon_color"/>
    
    <View
    android:id="@+id/snackbar"
    android:layout_width="0dp"
    android:layout_height="25dp"
    android:layout_marginTop="252dp"
    android:layout_marginBottom="8dp"
    android:layout_alignParentBottom="true"/>


-1
投票

添加 55dp 的底部边距,即 Admob 横幅的大小

此行位于浮动按钮或封闭布局中。

android:layout_marginBottom="55dp

© www.soinside.com 2019 - 2024. All rights reserved.