使用API21 + Toolbar
:
// Toolbar
Toolbar toolbar = new Toolbar(this);
toolbar.showOverflowMenu();
想彻底删除它的影子。 setElevation(0)
没有做任何事情,因为getElevation()
已经返回0
。
有材料设计参考:
https://material.io/guidelines/layout/structure.html#structure-toolbars
有开发参考:
https://developer.android.com/reference/android/widget/Toolbar.html
但我没有看到任何与阴影相关的信息。 Toolbar
问题:如何完全删除/隐藏Toolbar
阴影?
在工具栏上使用app:elevation="0dp"
而不是android:elevation
。如果它不起作用,将你的工具栏放在AppBarLayout
中并设置app:elevation="0dp"
:
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp">
...
</android.support.design.widget.AppBarLayout>
使用属性app:elevation="0dp"
到你的Toolbar
或AppBarLayout
去除阴影。
#。如果您仅使用Toolbar
,则将属性app:elevation="0dp"
添加到Toolbar
。
<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"
app:elevation="0dp"/>
#。如果你使用AppBarLayout
作为Toolbar
的容器,那么将属性app:elevation="0dp"
添加到AppBarLayout
。
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
app:elevation="0dp">
<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>
OUTPUT:
更新:
如果要以编程方式删除它,则可以使用以下代码:
getSupportActionBar().setElevation(0);
希望这会有所帮助〜
我自己找到了解决方案:
getActionBar().setElevation(0);
更多信息:
https://developer.android.com/reference/android/app/Activity.html#getActionBar()
而不是android:提升尝试app:提升
<android.support.design.widget.AppBarLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:elevation="0dp">
</android.support.design.widget.AppBarLayout>
如果您在工具栏中使用AppbarLayout,您可以在AppBar布局中使用app:elevation =“0dp”而不是android:elevation
将此代码放在Fragment
中,您要隐藏ToolBar
。
@Override
public void onResume() {
super.onResume();
((AppCompatActivity)getActivity()).getSupportActionBar().setElevation(0);
}
将“#00000000”设置为工具栏的backgroundColor,记住:如果你使用CoordinatorLayout和AppBarLayout,你应该删除你的contentView的
app:layout_behavior="@string/appbar_scrolling_view_behavior"
,我犯了这个基本错误。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
appBarLayout.outlineProvider = null
}