移除 Android ActionBar 底部阴影

9

我想在一个Activity中禁用ActionBar的阴影效果。如果使用这段代码,将会在整个应用程序中产生变化。

<style name="MyAppTheme" parent="android:Theme.Holo.Light">
    <item name="android:windowContentOverlay">@null</item>
</style>

我尝试了此代码,但它没有起作用。
getSupportActionBar().setElevation(0);

有什么建议吗?

3个回答

18

您可以为此设置自己的Activity样式:

<!-- Your main theme with ActionBar shadow. -->
<style name="MyAppTheme" parent="android:Theme.Holo.Light">
    ....
</style>

<!-- Theme without ActionBar shadow (inherits main theme) -->
<style name="MyNoActionBarShadowTheme" parent="MyAppTheme">
    <item name="windowContentOverlay">@null</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

因此,在Manifest.xml中,您可以为所有活动设置不同的样式:

<!-- Activity with ActionBar shadow -->
<activity
    android:name=".ShadowActivity"
    android:theme="@style/MyAppTheme"/>

<!-- Activity without ActionBar shadow -->
<activity
    android:name=".NoShadowActivity"
    android:theme="@style/MyNoActionBarShadowTheme"/>

或者你可以在onCreate()方法中以编程方式设置正确的主题:

或者您可以在onCreate()方法中以编程方式设置正确的主题:

@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.MyNoActionBarShadowTheme);
    super.onCreate(savedInstanceState);

    //...
}

1
我不知道我可以为活动设置不同的样式 :) - KiKo
有没有不使用 XML 而只通过代码实现相同功能的方法? - android developer

14

将app:elevation="0dp"添加到以下所示的appbarlayout中:

    <android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:theme="@style/AppTheme.AppBarOverlay"
    app:elevation="0dp">

    <android.support.v7.widget.Toolbar
        android:id="@+id/home_tool_bar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

8
创建一个继承你的App主题的新主题:
<style name="MyAppTheme.NoShadow" parent="MyAppTheme">
    <item name="android:windowContentOverlay">@null</item>
</style>

并让您的活动使用此主题。在您的清单中:

<activity
    android:name="...MyShadowlessActivity"
    android:theme="@style/MyAppTheme.NoShadow" .../>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接