Android(安卓)踩坑——FloatingActionButton自动添加边距
16lz
2021-01-24
前段时间用android的FloatingActionButton做一个返回顶部的按钮,原本以为挺简单,但是结果有点出乎意料。
布局很简单、
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="me.masteryi.trip.MainActivity"> <android.support.design.widget.FloatingActionButton xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_margin="16dp" android:src="@drawable/ic_add" app:fabSize="normal"/>RelativeLayout>
期待的效果是这样的
Android5.0以上的手机显示没什么问题,但是在5.0以下显示的边距会大一点
打开开发者选项发现,在android5.0以下fab会自动增加边距,而5.0以上则不会
所以,解决方案就是增加values-v21文件,在Android5.0以上和以下使用不同边距。
在valuse/dimens.xml中定义5.0以下使用尺寸
<dimen name="action_button_margin">0dpdimen>
在values-v21/dimens中定义5.0以上使用尺寸
<dimen name="action_button_margin">8dpdimen>
系统会根据不同系统加载不同dimen
还有,如果fab在CoordinatorLayout中不会出现刚才的问题,android5.0以上跟5.0以下显示效果相同。
更多相关文章
- Android(安卓)中自定义View的应用.
- 130292015012 陈月凤 第一章作业
- Android系统内存管理的问题
- Android(安卓)运行 C/C++程序 (使用 arm-linux-gcc 交叉编译环境
- Android(安卓)机器人:使用系统资源
- Google Android操作系统四大特点
- Android(安卓)广播
- NO.1 Android大概
- [毕设记录] Android照相机基础基于camera2API