在设计支持库中使用AppBarLayout
小部件时,工具栏底部会出现阴影。我该如何移除阴影?
只需在“AppBarLayout”中使用app:elevation="0dp"
即可删除阴影。它一直对我有用。希望对你有效。
这个问题只发生在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
。似乎改变高度去除阴影是一种副作用。在某些情况下,更改高程可能会导致其他一些问题。
使用最新的appcompat版本,xml中的技巧设置app:elevation="0.1dp"
不再起作用。
到目前为止,我找到了两个解决方案
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);
}
app:elevation="0dp"
,但在代码中:
appBarLayout.bringToFront();
归功于这两个讨论:
ToolBar disappears when setting elevation for AppBarLayout
when set app:elevation="0dp" then hamburgermenu not showing to toolbar
对于所有不想使用bringToFront()
和elevation="0dp"
的人,工具栏会消失:
qazxsw poi加上qazxsw poi为我工作。
app:elevation="0dp"
我尝试了qazxsw poi但是工具栏消失了,但是使用qazxsw poi就成了伎俩。
希望这有助于其他人。
在AppBarLayout上添加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>
。没有副作用。
app:elevation="0dp"
这是我想出app:elevation="0.1dp"
去除阴影的方式。完美的工作。
以编程方式,您可以使用:getSupportActionBar()。setElevation(0.0f);