[英]Android Floating Action Button wrong position despite gravity is set
I'm currently facing two different issues, both regarding fab gravity behavior. 我目前面临两个不同的问题,都与晶圆厂的重力行为有关。
In my layout I have a section consisting in a FrameLayout
with an ImageView
and a FloatingActionButton
as children views. 在我的布局中,我有一个由FrameLayout
组成的部分,其中有一个ImageView
和一个FloatingActionButton
作为子视图。
`<RelativeLayout ...>
*Here's a toolbar*
<ScrollView ...>
<LinearLayout ...>
<FrameLayout
android:id="@+id/prof_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/activity_vertical_margin">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/profile_image"
android:layout_width="@dimen/size_editing_pic"
android:layout_height="@dimen/size_editing_pic"
android:src="@drawable/ic_profilo_utente"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/floating_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:scaleType="center"
android:tint="@color/dark_gray"
app:layout_anchor="@id/profile_image"
app:layout_anchorGravity="bottom|end|right"
app:srcCompat="@drawable/ic_camera"
app:backgroundTint="@color/colorAccentEasy"
app:fabSize="mini"/>
</FrameLayout>
*Other sections*
</LinearLayout>
</ScrollView>
</RelativeLayout>`
The FAB is right where it should be but you can see that its shadow is clearly cut without the supposed fading. FAB是正确的,但是您可以看到它的阴影已被清楚地切掉而没有所谓的褪色。
The FAB is not where it's supposed to be. FAB不在应有的位置。
I tried anything that came to my mind and that I found online and here on SO. 我尝试了所有想到的事情,并在网上找到了这些内容。 I understand that it has to do with the different padding management that the core code does if the OS is pre or post Lollipop, and with the attribute useCompatPadding
(which I tried in different combinations), but I can't figure out how to successfully resolve it. 我知道,这与操作系统在Lollipop之前或之后的核心代码执行的不同填充管理以及属性useCompatPadding
(我尝试了不同的组合)有关,但是我无法弄清楚如何成功解决它。
Thanks to the suggestion I managed to solve Issue #1. 多亏了这个建议,我才得以解决问题#1。
As far as Issue #2 is concerned, though, I still can't solve it. 至于第二期,我仍然无法解决。 In my new fabStyle-v21 a layout_margins
of 5dp since it perfectly suited my needs. 在我的新fabStyle-v21中,由于它非常适合我的需求,所以layout_margins
为5dp。 I then tried putting a 0dp margin in the default style, but still nothing, even trying negative margins. 然后,我尝试将0dp页边距设置为默认样式,但还是一无所获,甚至尝试使用负页边距。
Issue 1- If you want to see the elevation
(shadow) of Views like FAB, Button
etc. you must leave space around them. 问题1-如果要查看FAB, Button
等视图的elevation
(阴影),则必须在其周围留出空间。 For example, you can set margins. 例如,您可以设置边距。 Using padding to the parent Layout of FAB
, will not work if there is no space between FAB
and edges of parent Layout 如果FAB
和父布局的边缘之间没有空格,则对FAB
的父布局使用填充是无效的
Issue 2- Floating Action Button has different margin and padding values before Lollipop devices. 问题2-浮动操作按钮在Lollipop设备之前具有不同的边距和填充值。 You can use different styles.xml files for different api levels. 您可以将不同的styles.xml文件用于不同的api级别。
styles.xml for different folders: 不同文件夹的styles.xml:
values-v21 值-v21
<style name="fabStyle">
<item name="android:layout_margin">16dp</item>
</style>
values (default) 值 (默认)
<style name="fabStyle">
<item name="android:layout_marginLeft">0dp</item>
<item name="android:layout_marginTop">0dp</item>
<item name="android:layout_marginRight">8dp</item>
<item name="android:layout_marginBottom">0dp</item>
</style>
usage: 用法:
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/fabStyle"
app:useCompatPadding="true"
app:elevation="6dp"/>
UPDATE: try adding app:useCompatPadding="true"
to your FAB without changing other arrangements. 更新:尝试将app:useCompatPadding="true"
到您的FAB中,而不更改其他安排。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.