2015-08-21 32 views

回答

135

只需使用app:elevation="0dp"删除阴影。它一直为我工作。希望对你有效。

+4

谢谢:)为我工作.. –

+2

我用android:提升< - 这不工作。 –

+16

请勿使用android:高程。使用应用程序:海拔。 – radley

1

我试过app:elevation="0dp",但工具栏消失,但使用app:elevation="0.1dp"发挥了诀窍。

希望这可以帮助别人。

+0

0.1dp也不起作用,它也隐藏了工具栏菜单。 –

+0

我有一个appcompat v23.0.1使用这个技巧的工作应用程序,你有什么版本? –

+0

我使用'v25.0.0'。 –

23

出现此问题时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看起来像改变高程去除阴影是一种副作用。在某些情况下更改高程可能会导致其他问题。

+0

感谢这。 – tim

+0

API只可用于版本21. – chakrapani

+0

这一个为我工作。谢谢! – Hilfritz

0

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

5

使用最新的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

+0

第二个解决方案适用于我和似乎很容易 –

1

在您的AppBarLayout上添加应用: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> 
0

编程您可以使用此: getSupportActionBar()setElevation(0。0F);