2015-08-21 35 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);