Remove action bar shadow programmatically

2019-02-08 06:36发布

How can i remove the drop shadow of action bar from java code ?.

If i remove from the style it is working fine.

<style name="MyTheme" parent="Theme.Sherlock">
....
<item name="windowContentOverlay">@null</item>
<item name="android:windowContentOverlay">@null</item>
....
</style>

But i need to remove and add it dynamically from java code.

7条回答
聊天终结者
2楼-- · 2019-02-08 06:48

I know this question is old, but this problem was new for me, so it might help somebody else as well.

Try:

  • getSupportActionBar().setElevation(0);

Or, if you are not using a custom action bar:

  • getActionBar().setElevation(0);

I think this is the easiest way to do it programmatically.

查看更多
老娘就宠你
3楼-- · 2019-02-08 06:50

Define a new style like this. Notice that there's no parent defined:

<style name="ConOver" >    <<== no parent
    <item name="android:windowContentOverlay">@null</item>
</style>

Add this above your code:

// Add this line before setContentView() call
// Lets you add new attribute values into the current theme
getTheme().applyStyle(R.style.ConOver, true);

// Rest stays the same
查看更多
叼着烟拽天下
4楼-- · 2019-02-08 06:54

There is no way to set value for windowContentOverlay attribute programmatically. But you can define two different themes, one for Activities with a visible ActionBar shadow and one for others:

<!-- Your main theme with ActionBar shadow. -->
<style name="MyTheme" parent="Theme.Sherlock">
    ....
</style>

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

Now you can set them to activities in AndroidManifest.xml:

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

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

    //...
}
查看更多
Summer. ? 凉城
5楼-- · 2019-02-08 06:54

If supportActionBar.elevation=0f did not work for you, You can remove the shadow by setting appBar.targetElevation=0f but this method is deprecated. The new method is using StateListAnimator.

You have to set the stateListAnimator to AppBarLayout without elevation that can be achieved by the steps below

  1. Create animator resource appbar_animator.xml

    <item
        android:state_enabled="true"
        app:state_collapsed="false"
        app:state_collapsible="true">
        <objectAnimator
            android:duration="150"
            android:propertyName="elevation"
            android:valueTo="0dp"
            android:valueType="floatType" />
    </item>
    
    <item android:state_enabled="true">
        <objectAnimator
            android:duration="150"
            android:propertyName="elevation"
            android:valueTo="0dp"
            android:valueType="floatType" />
    </item>
    
    <item>
        <objectAnimator
            android:duration="0"
            android:propertyName="elevation"
            android:valueTo="0"
            android:valueType="floatType" />
    </item>
    

    android.valueTo="0dp" defines the elevation.

  2. Inflate and set the StateListAnimator to appBar

    val stateAnimator=AnimatorInflater.loadStateListAnimator(this,R.animator.appbar_animator);
    appBar.stateListAnimator=stateAnimator
    

StateListAnimator was added in api 21 Lollipop

查看更多
啃猪蹄的小仙女
6楼-- · 2019-02-08 06:56

Declare 2 styles ne has a shadow and the other one doesn't. THen in your manifest define each activity's theme with respective one. e.g

<Activity 
android:theme="@style/ThemeShadow" ...>

...

<Activity 
android:theme="@style/ThemeNoShadow" ...>
查看更多
姐就是有狂的资本
7楼-- · 2019-02-08 06:58

I found this article: Change Theme of Layout Runtime by Button Click in Android

It allows you to change the theme of an activity, but it does call finish() and restart the activity. So, I am not sure it will work for what you want.

import android.app.Activity;
import android.content.Intent;

public class Utils
{
     private static int sTheme;

     public final static int THEME_DEFAULT = 0;
     public final static int THEME_WHITE = 1;
     public final static int THEME_BLUE = 2;

      /**
       * Set the theme of the Activity, and restart it by creating a new Activity of the same type.
       */
      public static void changeToTheme(Activity activity, int theme)
      {
           sTheme = theme;
           activity.finish();

 activity.startActivity(new Intent(activity, activity.getClass()));

      }

      /** Set the theme of the activity, according to the configuration. */
      public static void onActivityCreateSetTheme(Activity activity)
      {
           switch (sTheme)
                {
                default:
           case THEME_DEFAULT:
               activity.setTheme(R.style.FirstTheme);
               break;
           case THEME_WHITE:
               activity.setTheme(R.style.SecondTheme);
               break;
           case THEME_BLUE:
               activity.setTheme(R.style.Thirdheme);
               break;
           }
      }
 }
查看更多
登录 后发表回答