【问题标题】:ToolBar disappears when setting elevation for AppBarLayout为 AppBarLayout 设置高度时工具栏消失
【发布时间】:2016-08-16 07:47:39
【问题描述】:

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

<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

【问题讨论】:

  • 查看this帖子
  • @NightFury 谢谢。这解决了它。如果您可以发布答案,我会将其设置为已接受。

标签: android android-support-library


【解决方案1】:

@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 不起作用,整个布局正在消失。

【讨论】:

    【解决方案2】:

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

    您现在必须使用 stateListAnimator 属性来设置海拔。

    注意:在 StateListAnimator 中设置时长为 1ms,以避免在 Elevation Drawing 中出现延迟。

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

    【讨论】:

    • 此方法已弃用。目标海拔现在已弃用。 AppBarLayout 的高度现在通过 StateListAnimator 控制。如果通过此方法或 app:elevation 属性设置了目标海拔,则会创建一个使用给定海拔值的新状态列表动画器。
    • 这并不能解释整个工具栏是如何消失的。即使方法和属性折旧,它也应该可以工作。
    猜你喜欢
    • 1970-01-01
    • 2020-05-04
    • 2017-02-27
    • 1970-01-01
    • 1970-01-01
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多