【问题标题】:How to set the elevation of an AppBarLayout programmatically in the Android Support Library v24.0.0?如何在 Android 支持库 v24.0.0 中以编程方式设置 AppBarLayout 的高度?
【发布时间】:2016-10-25 19:57:49
【问题描述】:

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

appBayLayout.setElevation(0);

在 XML 中设置海拔时确实有效。

【问题讨论】:

    标签: android android-support-library android-support-design


    【解决方案1】:

    编辑

    v24.0.0 中的AppBarLayout 使用StateListAnimator,根据其状态定义海拔。因此,如果正在使用 StateListAnimator(默认情况下发生),则使用 setElevation 将无效。通过 XML 或以编程方式设置 elevation(均适用于 API >= 21):

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

    旧答案

    这似乎是issue of the design support library。该问题与使用setElevation 以编程方式设置高程的方式有关。从 XML 设置它是在视图中放置 StateListAnimator,而不是调用 setElevation。但是,setElevation 应该可以工作。

    这里有一个解决方法:

    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 中调用类 ViewUtilsLollipop 中的方法。

    【讨论】:

    • 您的示例中使用了哪个view stateListAnimator.addState(new int[0], ObjectAnimator.ofFloat(view, "elevation", 0));?
    • @VolodymyrKhodonovych 作为参数传递的那个
    • 我在 android 21 中有这样的错误 > JNI DETECTED ERROR IN APPLICATION: can't call void > android.view.View.setElevation(float) on null object
    • @VolodymyrKhodonovych 看来您的视图对象引用为空
    【解决方案2】:

    另一种可能的解决方案是将 android:stateListAnimator="@null" 添加到您的 AppBarLayout,如下所示。

    <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:stateListAnimator="@null"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    

    【讨论】:

    • 我使用appBarLayout.setElevation(0) 和appBarLayout.setStateListAnimator(null) 禁用提升,使用appBarLayout.setElevation(10.5) 启用。它在运行时和屏幕旋转后工作
    【解决方案3】:

    就我而言,我需要在运行时更改AppBarLayout 的高度,而setElevation(..) 做到了。

    然而,在屏幕旋转并从onCreateOptionMenu 调用setElevation(..) 后不起作用,但setStateListAnimator(null) 成功了。

    因此我得到了这个逻辑:

    public final float appBarElevation = 10.5f;
    
    public void disableAppBarElevation() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            appBarLayout.setElevation(0);
            appBarLayout.setStateListAnimator(null);
        }
    }
    
    public void enableAppBarElevation() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            appBarLayout.setElevation(appBarElevation);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-09-30
      • 2015-12-09
      • 2023-03-11
      • 1970-01-01
      • 2012-12-08
      • 2016-10-23
      • 2014-08-22
      • 1970-01-01
      • 2012-03-02
      相关资源
      最近更新 更多