【问题标题】:remove shadow below AppBarLayout widget android删除 AppBarLayout 小部件 android 下方的阴影
【发布时间】:2015-11-15 00:22:51
【问题描述】:

在设计支持库中使用AppBarLayout 小部件时,工具栏底部会出现阴影。我怎样才能消除那个阴影?

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    只需在“AppBarLayout”中使用app:elevation="0dp" 即可移除阴影。它一直对我有用。希望它对你有用。

    【讨论】:

    • 不要使用 android:elevation。使用 app:elevation。
    • 有没有办法以编程方式执行此操作而不会收到设置高程仅在 L 之后可用的警告?
    • app:elevation="0dp",阴影被移除,但现在标签不可点击。
    • 将其设置为 0dp 会隐藏工具栏。
    • 不幸的是不再是一个有效的答案。请参阅下面的刘腾回答setOutlineProvider
    【解决方案2】:

    这个问题只有在api版本>=21的时候才会出现,如果不想改变海拔,可以使用:

    appBar.setOutlineProvider(null);
    

    记得查看api版本


    编辑:

    下面是setOutlineProvider的源码。

       /**
         * Sets the {@link ViewOutlineProvider} of the view, which generates the Outline that defines
         * the shape of the shadow it casts, and enables outline clipping.
         * <p>
         * The default ViewOutlineProvider, {@link ViewOutlineProvider#BACKGROUND}, queries the Outline
         * from the View's background drawable, via {@link Drawable#getOutline(Outline)}. Changing the
         * outline provider with this method allows this behavior to be overridden.
         * <p>
         * If the ViewOutlineProvider is null, if querying it for an outline returns false,
         * or if the produced Outline is {@link Outline#isEmpty()}, shadows will not be cast.
         * <p>
         * Only outlines that return true from {@link Outline#canClip()} may be used for clipping.
         *
         * @see #setClipToOutline(boolean)
         * @see #getClipToOutline()
         * @see #getOutlineProvider()
         */
        public void setOutlineProvider(ViewOutlineProvider provider) {
            mOutlineProvider = provider;
            invalidateOutline();
        }
    

    据说If the ViewOutlineProvider is null, if querying it for an outline returns false, or if the produced Outline is {@link Outline#isEmpty()}, shadows will not be cast.

    所以,如果你想去除阴影,你最好使用这个方法而不是设置app:elevation 看起来改变高度去除阴影是一种副作用。在某些情况下,更改海拔可能会导致其他一些问题。

    【讨论】:

    • API 仅适用于版本 21。
    • API
    • 只有当 api >= 21 时才会出现这个问题
    【解决方案3】:

    对于所有不想使用bringToFront()elevation="0dp" 的人来说,工具栏会消失:

    app:elevation="0dp" 结合android:translationZ="0.1dp" 为我工作。

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:elevation="0dp"
        android:translationZ="0.1dp"
        >
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@null"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>
    
    </android.support.design.widget.AppBarLayout>
    

    【讨论】:

      【解决方案4】:

      对于最新的 appcompat 版本,xml 中的技巧设置 app:elevation="0.1dp" 不再起作用。

      到目前为止,我已经找到了两种解决方案。

      1. 尝试使用 stateListAnimator,而不是设置 app:elevation。例如在代码中:

        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);
        }
        
      2. 更简单的方法是您仍然像往常一样在 xml 中设置 app:elevation="0dp",但在代码中:

        appBarLayout.bringToFront();
        

      归功于这两个讨论:

      ToolBar disappears when setting elevation for AppBarLayout

      when set app:elevation="0dp" then hamburgermenu not showing to toolbar

      【讨论】:

        【解决方案5】:

        使用android:stateListAnimator="@null"。没有副作用。

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:animateLayoutChanges="true"
            android:stateListAnimator="@null"
            >
        

        【讨论】:

        • 谢谢!有用。 app:elevation="0dp", android:elevation="0dp" 和 android:outlineProvider="none" 都不适合我。
        【解决方案6】:

        我尝试了app:elevation="0dp",但工具栏消失了,但使用app:elevation="0.1dp" 成功了。

        希望这对其他人有所帮助。

        【讨论】:

        • 0.1dp 也不起作用,它也隐藏了工具栏菜单。
        • 我有一个使用这个提示的 appcompat v23.0.1 的工作应用程序,你有什么版本?
        • 我正在使用v25.0.0
        • 它似乎不再适用于更新版本:(。
        【解决方案7】:

        在您的 AppBarLayout 上添加 app:elevation="0dp"。像这个例子

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:elevation="0dp"
            android:theme="@style/AppTheme.AppBarOverlay">
        
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />
        
        </android.support.design.widget.AppBarLayout>
        

        【讨论】:

        • 救命答案:)
        【解决方案8】:

        您可以通过编程方式使用它: getSupportActionBar().setElevation(0.0f);

        【讨论】:

          【解决方案9】:

          这就是我想出的app:elevation="0dp" 去除阴影的方法。非常有效。

          【讨论】:

            【解决方案10】:

            我在 Kotlin minSdkVersion 21 上使用 AppBarLayout 和 TabLayout 执行此操作,所以:app:elevation="0dp" 确实帮助我解决了阴影问题,但使用此解决方案使我无法按下 TabLayout 上的按钮。

            所以结合 app:elevation="0.1dp"app:elevation="0dp"i 可以修复阴影并且仍然能够与我的标签布局进行交互。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2012-08-28
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-04-29
              • 2016-08-06
              • 1970-01-01
              相关资源
              最近更新 更多