Removing bottom shadow on ActionBar - Android

前提是你 提交于 2019-12-04 00:46:31

问题


I want to disable ActionBar shadow but only in one Activity. If I use this code it will be change in whole aplication.

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

I tried this code, but it is not working

getSupportActionBar().setElevation(0);

Any suggestion...?


回答1:


You can set your own style for Activity for this:

<!-- 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>

So in Manifest.xml you can set different style for all Activities:

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

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

Or you can set the right theme programmatically in onCreate() method:

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

    //...
}



回答2:


Add app:elevation="0dp" to appbarlayout as shown below:

    <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>



回答3:


Create a new Theme that inherits your App theme:

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

and let your activity use this theme. In your Manifest:

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


来源:https://stackoverflow.com/questions/27170718/removing-bottom-shadow-on-actionbar-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!