如何在Android支持库v24.0.0中以编程方式设置AppBarLayout的海拔高度? [英] How to set the elevation of an AppBarLayout programmatically in the Android Support Library v24.0.0?

查看:59
本文介绍了如何在Android支持库v24.0.0中以编程方式设置AppBarLayout的海拔高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Android支持库v23.4.0升级到v24.0.0时,以编程方式将海拔高度设置为0的AppBarLayout停止工作:

When upgrading from the Android Support Library v23.4.0 to v24.0.0, setting the elevation to 0 programmatically to an AppBarLayout stopped working:

appBayLayout.setElevation(0);

在XML中设置高程时确实起作用。

It does work when setting the elevation in the XML.

推荐答案

编辑

AppBarLayout 使用 StateListAnimator 来定义高程,具体取决于其状态。因此,如果使用 StateListAnimator (默认情况下发生),则使用 setElevation 将无效。通过XML或以编程方式(<对于API> = 21)设置高度

The AppBarLayout from v24.0.0 uses a StateListAnimator that defines the elevation depending on its state. So using setElevation will have no effect if a StateListAnimator is being used (which happens by default). Set the elevation via the XML or programmatically (both for API >= 21):

StateListAnimator stateListAnimator = new StateListAnimator();
stateListAnimator.addState(new int[0], ObjectAnimator.ofFloat(view, "elevation", 0));
appBarLayout.setStateListAnimator(stateListAnimator);

旧答案

这似乎是设计支持库的问题。问题与使用 setElevation 以编程方式设置高程的方式有关。从XML进行设置是在视图中放置 StateListAnimator 而不调用 setElevation 。但是, setElevation 应该可以。

This seems to be an issue of the design support library. The problem is related to the way the elevation is set programmatically, using setElevation. Setting it from the XML is placing a StateListAnimator in the view and not calling setElevation. However, setElevation should work.

这里有一个解决方法:

setDefaultAppBarLayoutStateListAnimator(appBarLayout, 0);

@SuppressLint("PrivateResource")
private static void setDefaultAppBarLayoutStateListAnimator(final View view, final float targetElevation) {
    final StateListAnimator sla = new StateListAnimator();

    // Enabled, collapsible and collapsed == elevated
    sla.addState(new int[]{android.R.attr.enabled, android.support.design.R.attr.state_collapsible,
            android.support.design.R.attr.state_collapsed},
            ObjectAnimator.ofFloat(view, "elevation", targetElevation));

    // Enabled and collapsible, but not collapsed != elevated
    sla.addState(new int[]{android.R.attr.enabled, android.support.design.R.attr.state_collapsible,
            -android.support.design.R.attr.state_collapsed},
            ObjectAnimator.ofFloat(view, "elevation", 0f));

    // Enabled but not collapsible == elevated
    sla.addState(new int[]{android.R.attr.enabled, -android.support.design.R.attr.state_collapsible},
            ObjectAnimator.ofFloat(view, "elevation", targetElevation));

    // Default, none elevated state
    sla.addState(new int[0], ObjectAnimator.ofFloat(view, "elevation", 0));

    view.setStateListAnimator(sla);
}

这是从构造函数的操作中获取的,在类中调用方法< v24.0.0中的code> ViewUtilsLollipop

This is taken from what the constructor does, calling a method in the class ViewUtilsLollipop in v24.0.0.

这篇关于如何在Android支持库v24.0.0中以编程方式设置AppBarLayout的海拔高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆