Android:AppBarLayout设置elevation为0,结果消失

Android:AppBarLayout设置elevation为0,结果消失,第1张

Android:AppBarLayout设置elevation为0,结果消失

Android:AppBarLayout设置elevation为0,结果消失
  • 项目场景
  • 问题描述
  • 解决方法

项目场景

将AppBarLayout中的ToolBar嵌入背景中,没有边框和阴影,融为一体。效果如下所示:



问题描述

为解决应用栏的边框和阴影效果,使用app:elevation=0将应用栏的高程设置为0,结果应用栏整体消失。效果如下所示:



解决方法

据文档所述:该方法已被弃用,应当使用StateListAnimator属性。在对应的Activity中添加如下代码:

setTargetElevation
public void setTargetElevation (float elevation)
This method isdeprecated. target elevation is now deprecated. AppBarLayout’selevation is now controlled via a StateListAnimator. If a targetelevation is set, either by this method or the app:elevation attribute, a new state list animator is created which uses the given elevation value.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StateListAnimator stateListAnimator = new StateListAnimator();
            stateListAnimator.addState(new int[0], ObjectAnimator.ofFloat(binding.appbar,"elevation",0.1f));
            binding.appbar.setStateListAnimator(stateListAnimator);
        }

PS:项目采用DataBinding,因此直接使用binding.appbar,未使用DataBinding者可用findViewById()


参考文章:ToolBar disappears when setting elevation for AppBarLayout

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5481621.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-12
下一篇2022-12-12

发表评论

登录后才能评论

评论列表(0条)

    保存