栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/489495.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号