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
|