为 AppBarLayout 设置高度时工具栏消失

ToolBar disappears when setting elevation for AppBarLayout

我的 ToolBar 在为 AppBarLayout 设置高度时消失了。这是布局。

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/appbar_height"
    app:elevation="0dp"
    android:background="@color/transparent">

    <android.support.v7.widget.Toolbar
        style="@style/ToolBarStyle"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:elevation="0dp"
        android:background="@drawable/backgorund_toolbar_tranluscent"
        android:minHeight="@dimen/abc_action_bar_default_height_material" />
</android.support.design.widget.AppBarLayout>

我已经为 app:elevation 尝试了 0dp、0.1dp 和 4dp 等值。这里发生了什么事?它是支持库错误吗?我正在使用 24.0.0.

新更新:在 Appcompat v24.0.0 中,您不能使用 setElevation() 和 app:elevation 将高度设置为 AppBarLayout,因为它们已被弃用。

您现在必须使用 stateListAnimator 属性 来设置高度。

注意:在StateListAnimator中将持续时间设置为1ms,以避免立面图延迟。

AppBarLayout 高度更改在 appCompat v24.0.0 上延迟

@Zeeshan 的回答完全正确。

这里还有一个可以运行的示例代码

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

我必须将高度设置为 0.1,因为将它设置为 0 不起作用,整个布局都消失了。