Android——FloatingActionButton使用说明以及避坑

1、简介

1.1、 com.android.support库下

 compile 'com.android.support:design:26.0.0'

1.2、material库下

com.google.android.material.floatingactionbutton.FloatingActionButton 库下

implementation 'com.google.android.material:material:1.1.0'
 

 官方地址:  FloatingActionButton  |  Android Developers

1.3、 哪些操作推荐使用FloatingActionButton

如: 添加,收藏,编辑,分享,导航等,如下图:

2、属性介绍

XML 属性

android:src中心显示的图标
app:layout_anchor设置FAB的锚点,即以哪个控件为参照设置位置
app:layout_anchorGravityFAB相对于锚点的位置
app:backgroundTintFAB按钮边框颜色
app:borderWidthFAB按钮边框大小
android:backgroundTint是里面背景颜色
android:tint是里面图标背景
FloatingActionButton_elevationapp:elevation阴影的高度
FloatingActionButton_ensureMinTouchTargetSize
FloatingActionButton_fabCustomSize
FloatingActionButton_fabSizeapp:fabSize按钮大小,normal (56dp) mini(40dp)
FloatingActionButton_hideMotionSpec
FloatingActionButton_hoveredFocusedTranslationZ
FloatingActionButton_maxImageSize
FloatingActionButton_pressedTranslationZapp:pressedTranslationZ点击按钮时,按钮边缘阴影的宽度,通常设置比elevation的数值大!
FloatingActionButton_rippleColorapp:rippleColor按压水波纹颜色
FloatingActionButton_showMotionSpec
FloatingActionButton_useCompatPadding

3、使用例子

FloatingActionButton基本使用及踩坑记录_gaolhjy的博客-CSDN博客_floatingactionbutton

4、闭坑问题

4.1、 图标颜色无法改变

4.1.1、 复现场景

xml中引入图标后,默认是黑色或其他颜色,无法修改自己想要的颜色。

比如我们期望 白色的 中心图标,但是常常是 黑色或者其他颜色的中心图标

4.1.2、 其中一种方式colorOnSecondary

网上博客大多数说的是  theme中 colorOnSecondary的颜色决定了图标的颜色。

【Android】使用FloatingActionButton过程中底图颜色始终为黑色_victor-维克特的博客-CSDN博客_android floatingactionbutton 颜色

FloatingActionButton图标颜色更改不了_github_39465103的博客-CSDN博客

这位楼主直接让大家不要用某类主题,哈哈,这是头痛医脚

FloatingActionButton图标颜色无法修改 - 简书

上面的方法也不是万能,主要原因还是取决于 你是用的什么theme,并非所有主题都是通过 colorOnSecondary的颜色决定的。

4.1.3、 另一种方式android:tint

直接设置该属性,效果立竿见影
android:tint="@color/red_btn_bg_color" 是里面图片背景
 

ViewPager是一个Android中的View容器,可以让用户在多个页面之间进行滑动切换。要实现多页面滑动切换以及动画效果,可以按照以下步骤: 1. 在XML布局文件中添加ViewPager控件,并添加对应的布局文件,例如: ``` <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 2. 在Java代码中为ViewPager设置Adapter,用于显示多个页面。例如: ``` ViewPager viewPager = findViewById(R.id.viewPager); MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager()); viewPager.setAdapter(adapter); ``` 其中,MyPagerAdapter是自定义的PagerAdapter类,需要继承自FragmentPagerAdapter或FragmentStatePagerAdapter。 3. 在自定义的PagerAdapter类中实现getItem()方法,用于返回每个页面的Fragment实例。例如: ``` @Override public Fragment getItem(int position) { switch (position) { case 0: return new Fragment1(); case 1: return new Fragment2(); case 2: return new Fragment3(); default: return null; } } ``` 其中,Fragment1、Fragment2、Fragment3是自定义的Fragment类,用于显示对应页面的内容。 4. 如果需要添加页面切换的动画效果,可以在Java代码中为ViewPager设置PageTransformer。例如: ``` viewPager.setPageTransformer(true, new DepthPageTransformer()); ``` 其中,DepthPageTransformer是自定义的PageTransformer类,用于实现页面切换时的动画效果。可以参考以下代码示例: ``` public class DepthPageTransformer implements ViewPager.PageTransformer { private static final float MIN_SCALE = 0.75f; public void transformPage(View view, float position) { int pageWidth = view.getWidth(); if (position < -1) { // [-Infinity,-1) // This page is way off-screen to the left. view.setAlpha(0f); } else if (position <= 0) { // [-1,0] // Use the default slide transition when moving to the left page view.setAlpha(1f); view.setTranslationX(0f); view.setScaleX(1f); view.setScaleY(1f); } else if (position <= 1) { // (0,1] // Fade the page out. view.setAlpha(1 - position); // Counteract the default slide transition view.setTranslationX(pageWidth * -position); // Scale the page down (between MIN_SCALE and 1) float scaleFactor = MIN_SCALE + (1 - MIN_SCALE) * (1 - Math.abs(position)); view.setScaleX(scaleFactor); view.setScaleY(scaleFactor); } else { // (1,+Infinity] // This page is way off-screen to the right. view.setAlpha(0f); } } } ``` 以上就是实现Android中ViewPager多页面滑动切换以及动画效果的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青苔猿猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值