删除AppBarLayout小部件android下面的阴影

Abd*_*man 78 android android-layout

AppBarLayout在设计支持库中使用窗口小部件时,工具栏底部会显示阴影.我该如何移除阴影?

dan*_*d94 197

只需使用app:elevation="0dp"去除阴影.它一直对我有用.希望对你有效.

  • 不要使用android:elevation.使用app:elevation. (47认同)
  • 将其设置为0dp隐藏了工具栏. (8认同)
  • 我使用android:elevation < - 这不起作用. (6认同)
  • 有没有办法以编程方式执行此操作而不会收到设置高程仅在L之后可用的警告? (3认同)
  • app:elevation =“ 0dp”,阴影已删除,但现在选项卡不可单击。 (2认同)
  • 不幸的是不再是一个有效的答案。请参阅下面刘腾的回答“setOutlineProvider” (2认同)

Liu*_*eng 44

api版本> = 21时会出现此问题,如果您不想更改高程,可以使用:

appBar.setOutlineProvider(null);
Run Code Online (Sandbox Code Playgroud)

记得检查api版本


编辑:

Blow是源代码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();
    }
Run Code Online (Sandbox Code Playgroud)

有人说 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.似乎改变高度去除阴影是一种副作用.在某些情况下,更改高程可能会导致其他一些问题.


小智 8

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

到目前为止,我找到了两个解决方案

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

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

    appBarLayout.bringToFront();
    
    Run Code Online (Sandbox Code Playgroud)

归功于这两个讨论:

为AppBarLayout设置高程时,ToolBar会消失

当设置app:elevation ="0dp"然后hamburgermenu没有显示到工具栏


fup*_*uck 7

对于那些不想使用谁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>
Run Code Online (Sandbox Code Playgroud)


Sof*_*ion 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"
    >
Run Code Online (Sandbox Code Playgroud)