简体   繁体   English

我们如何将浮动操作按钮放在任何布局的顶部

[英]How we can put Floating action button on top of any layout

I am doing some operation on floating action button.我正在对浮动操作按钮进行一些操作。 I have a list view and showing google ad at bottom of my screen, The floating action button is showing at same place (bottom|end).我有一个列表视图并在屏幕底部显示谷歌广告,浮动操作按钮显示在同一位置(底部|末端)。 so action button is hiding the ad.所以操作按钮隐藏了广告。

I want shift the Floating action button little above the ad linear layout.我想将浮动操作按钮移动到广告线性布局上方一点。

Please have a look the image:请看图片: 在此处输入图像描述

My code is look like:我的代码是这样的:

<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

Please help.请帮忙。

Thanks in advance.提前致谢。

You should use RelativeLayout , and as we know, later children in a RelativeLayout tend to float over earlier children in a RelativeLayout .您应该使用RelativeLayout ,正如我们所知, RelativeLayout RelativeLayout前面的孩子之上。

So, to have a FAB float over of any layout, make sure that the FAB is defined after all views (at the end of the XML layout).因此,要让 FAB 浮动在任何布局之上,请确保在所有视图之后(在 XML 布局的末尾)定义 FAB。

If you use android:layout_margin="@dimen/fab_margin" the best way to manipulate FAB is set dependencies between views, for example:如果您使用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>

Floating action button always will be above the ad linear layout, and independent from ad layout size.浮动操作按钮始终位于广告线性布局之上,并且独立于广告布局尺寸。 Hope it helps.希望能帮助到你。

<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"
       />

These two lines inside the fab button helped me solved this problem: fab 按钮内的这两行帮助我解决了这个问题:

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

Here's the entire code I used for the button alone:这是我单独用于按钮的完整代码:

 <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" />

THESE TWO LINES ARE IMPORTANT.这两行很重要。

android:layout_alignParentEnd="true" android:layout_alignParentEnd="真"

android:layout_alignParentBottom="true"机器人:layout_alignParentBottom =“真”

Add a bottom Margin of 55dp which is the size of Admob Banners添加 55dp 的底部边距,这是 Admob 横幅的大小

This line in either the floating button or enclosing layout.此行位于浮动按钮或封闭布局中。

android:layout_marginBottom="55dp

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

相关问题 如何在浮动操作按钮上方放置透明布局 - how to put a transparent layout above floating action button Android:我们如何在 xml 布局中隐藏/显示浮动操作按钮(fab) - Android: How can we hide/show floating action button(fab) in xml layout 始终将浮动动作按钮置于顶部 - Put the Floating action button on Top always Snackbar顶部的浮动动作按钮。 怎么样? - Floating Action Button on top of Snackbar. How? 如何在滚动视图顶部添加浮动操作按钮 - How to add floating action button on top of scrollview 浮动操作按钮在相对布局中不浮动 - Floating action button is not floating in relative layout 不能将浮动操作按钮放在两个布局的中间 - can't put the floating action button in the middle of two layouts 如何在父母布局的右上角添加两个浮动操作按钮(一半为内部布局,一半为外部),并在单击时显示 - How to add two floating action button on top-right of parrent layout (half insidelayout, and half outside), and show on click React Native:由于溢出=在Android上隐藏,我们如何设计浮动操作按钮 - React Native: as overflow = hidden on Android, how can we design floating action button 如何从底部应用程序栏布局隐藏浮动操作按钮 - How to hide the Floating Action Button from Bottom App Bar Layout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM