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

问题描述 投票:68回答:9

在设计支持库中使用AppBarLayout小部件时,工具栏底部会出现阴影。我该如何移除阴影?

android android-layout
9个回答
183
投票

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


38
投票

这个问题只发生在api版本> = 21时,如果你不想改变高程,你可以使用:

appBar.setOutlineProvider(null);

记得检查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();
    }

据说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版本,xml中的技巧设置app:elevation="0.1dp"不再起作用。

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

  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); }
  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


6
投票

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

qazxsw poi加上qazxsw poi为我工作。

app:elevation="0dp"

2
投票

我尝试了qazxsw poi但是工具栏消失了,但是使用qazxsw poi就成了伎俩。

希望这有助于其他人。


1
投票

在AppBarLayout上添加app:elevation =“0dp”。像这个例子

android:translationZ="0.1dp"

1
投票

使用<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> 。没有副作用。

app:elevation="0dp"

0
投票

这是我想出app:elevation="0.1dp"去除阴影的方式。完美的工作。


0
投票

以编程方式,您可以使用:getSupportActionBar()。setElevation(0.0f);

© www.soinside.com 2019 - 2024. All rights reserved.