我为棒棒糖添加了一个FloatingActionButton
,并且在它上面完全没问题,但是对于棒棒糖以下它需要额外的空间来覆盖它
XML code:
<android.support.design.widget.FloatingActionButton
android:id="@+id/bt_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:gravity="center"
android:src="@drawable/ic_done"
app:backgroundTint="@color/primary"
app:borderWidth="0dp"
app:fabSize="mini" />
上面的棒棒糖n的截图:
这里的FloatingActionButton
占据了适当的空间,但是看第二个屏幕
pre-lollipop的屏幕截图:现在看一下qazxsw poi如何获得额外的空白或额外空白。如何解决此问题。
将FloatingActionButton
放入FloatingActionButton
,所有平台上的所有边距都相同。在这种情况下,按钮的位置由CoordinatorLayout
调整。
FloatingActionButton.Behavior
根据对<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:id="@+id/bt_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="10dp"
app:backgroundTint="@color/primary"
app:borderWidth="0dp"
app:fabSize="mini"/>
</android.support.design.widget.CoordinatorLayout>
的评论,这需要android支持库v22.2.1
这是一个已知问题,这是由于额外的利润。你可以像下面这样做,
android issue 175330
//to fix margin issue/bug with pre lollipop version
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) floatingActionButton.getLayoutParams();
params.setMargins(0, 0, 0, 0); // get rid of margins since shadow area is now the margin
floatingActionButton.setLayoutParams(params);
}
没有什么花哨但只是一个bug。
我已经使用负边距来缩小前棒棒糖设备上的FAB尺寸,但也保留了按钮周围的阴影区域,因此视图不会被破坏。
<android.support.design.widget.FloatingActionButton
android:id="@+id/fabBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:src="@drawable/ic_plus"
app:fabSize="normal" />
另外我用Fazxswpoi将FAB包装在LineaLayout中:
int dimen5dp = getResources().getDimensionPixelSize(R.dimen.dimen_5_dp);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) floatingActionButton.getLayoutParams();
params.setMargins(
params.leftMargin,
params.topMargin - ((int) (dimen5dp * 3.1)), //Approximate factor to shrink the extra
params.rightMargin - ((int) (dimen5dp * 3.1)), //spacing by the shadow to the actual
params.bottomMargin - ((int) (dimen5dp * 3.1))); //size of the FAB without a shadow
floatingActionButton.setLayoutParams(params);
}
下面是左边的结果图像,它是棒棒糖前设备,右边是棒棒糖和上面的设备。我使用了uiautomatorviewer并打开了设备上的布局边界。红色虚线是FAB按钮的边界,而带有蓝色角的紫色线是LinearLayout的边界。在棒棒糖设备的情况下,FAB的阴影没有占用额外的空间,因此它的界限正好在按钮周围而没有额外的边距。在前棒棒糖设备中,阴影占用额外空间,因此FAB边界将在按钮周围具有边距,但设置了负边距时,LinearLayout缩小到按钮的边界,而没有阴影添加的额外空间。
app:useCompatPadding="false"
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="RtlHardcoded">
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
app:backgroundTint="@color/white"
app:srcCompat="@drawable/ic_expand"
app:useCompatPadding="false"
tools:ignore="RtlHardcoded"/>
</LinearLayout>