我们如何将“浮动操作"按钮放置在任何布局的顶部 [英] How we can put Floating action button on top of any layout
问题描述
我正在对浮动操作按钮进行一些操作.我有一个列表视图,并在屏幕底部显示google广告,该浮动操作按钮显示在同一位置(底部|末尾).因此操作按钮隐藏了广告.
我想将浮动操作"按钮移到广告线性布局上方一点.
请查看图片:
我的代码如下:
<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浮动在任何布局上,请确保在所有视图之后(在XML布局的末尾)定义了FAB.
如果使用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>
浮动操作按钮将始终位于广告线性布局上方,并且不受广告布局尺寸的影响. 希望对您有所帮助.
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
.
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).
If you use android:layout_margin="@dimen/fab_margin"
the best way to manipulate FAB is set dependencies between views, for example:
<?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.
这篇关于我们如何将“浮动操作"按钮放置在任何布局的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!