Extra margin(spacing) around FloatingActionButton only on API 19

和自甴很熟 提交于 2019-12-23 19:04:17

问题


I'm experiencing an extra margin or spacing around FloatingActionButton but only on API19.

Screenshot on API19:

Margin is correct on every other version, see screenshot below:

The developer option for showing layout boundaries is turned on in both cases. You can clearly see that in API 19 there is an extra space around the FABs.

XML:

      <RelativeLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">

                        <android.support.design.widget.FloatingActionButton
                            android:id="@+id/path_btn"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="12dp"
                            android:layout_marginRight="12dp"
                            android:layout_marginTop="12dp"
                            android:background="@null"
                            app:backgroundTint="@color/blue_light"
                            app:srcCompat="@drawable/ic_line" />

                        <android.support.design.widget.FloatingActionButton
                            android:id="@+id/stream_toggle_btn"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/path_btn"
                            android:layout_marginBottom="12dp"
                            android:layout_marginLeft="12dp"
                            android:layout_marginTop="12dp"
                            android:background="@null"
                            app:srcCompat="@drawable/ic_stream_video_white" />
                    </RelativeLayout>

Please consider that the margins in the XML only adds the purple area on the screenshots. If I remove the margins the extra spacing does not disappear.

Please help if you can.

Thanks.

E D I T:

Adding

  app:useCompatPadding="true"

To FABS does not help. Spacing still there.


回答1:


You can programmatically remove margin from floatingActionButton like.It is a known issue and it is because of extra margin.

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) stream_toggle_btn.getLayoutParams();
    params.setMargins(0, 0, 0, 0); 
    stream_toggle_btn.setLayoutParams(params);

    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) path_btn.getLayoutParams();
    params.setMargins(0, 0, 0, 0); 
    path_btn.setLayoutParams(params);
}

EDIT

Try to use this properties inside FloatingActionButton xml.

app:elevation="0dp"
app:pressedTranslationZ="0dp"

Like

<android.support.design.widget.FloatingActionButton
    android:id="@+id/path_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="12dp"
    android:layout_marginRight="12dp"
    android:layout_marginTop="12dp"
    android:background="@null"
    app:backgroundTint="@color/blue_light"
    app:srcCompat="@drawable/ic_line"
    app:elevation="0dp"
    app:pressedTranslationZ="0dp"/>



回答2:


This is because special padding implementation in the FAB on pre-Lollipop devices.

You can use

app:useCompatPadding="true"

to override this behavior.

boolean: true if FloatingActionButton is adding inner padding on platforms Lollipop and after, to ensure consistent dimensions on all platforms.




回答3:


Set app:useCompatPadding="true" for the fab if you want the same padding for all android versions



来源:https://stackoverflow.com/questions/51243882/extra-marginspacing-around-floatingactionbutton-only-on-api-19

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!