以编程方式删除操作栏阴影

问题描述 投票:15回答:7

如何从java代码中删除操作栏的阴影?

如果我从样式中删除它工作正常。

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

但我需要从java代码中动态删除和添加它。

android styles android-actionbar android-theme dropshadow
7个回答
12
投票

无法以编程方式为windowContentOverlay属性设置值。但是您可以定义两个不同的主题,一个用于具有可见ActionBar阴影的活动,另一个用于其他主题:

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

现在您可以将它们设置为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"/>

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

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

    //...
}

8
投票

如果你不会制作更多主题,那就试试吧。这对我很有用!

public void disableActionbarShadow() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        View v = getActivity().findViewById(android.R.id.content);
        if (v instanceof FrameLayout) {
            ((FrameLayout) v).setForeground(null);
        }
    }else {
        // kitkat.
        // v is ActionBarOverlayLayout. unfortunately this is internal class.
        // if u want to check v is desired class, try this
        //   if(v.getClass().getSimpleName.equals("ActionBarOverlayLayout")) 
        // (we cant use instanceof caz ActionBarOverlayLayout is internal package)
        View v = ((ViewGroup)getActivity().getWindow().getDecorView()).getChildAt(0);
        v.setWillNotDraw(true);
    }
}

从Kitkat(可能),ActionBarOverlayLayout包含在活动的视图树中。 这显示了actionbar shadow(我认为是XD)

refs:http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/com/android/internal/widget/ActionBarOverlayLayout.java#79

Becaraful我不知道如果使用支持库版本会发生什么。


4
投票

定义这样的新样式。请注意,没有定义父级:

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

在代码上方添加:

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

3
投票

声明2个样式ne有阴影而另一个没有。在您的清单中,用相应的一个定义每个活动的主题。例如

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

...

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

2
投票

我找到了这篇文章:Change Theme of Layout Runtime by Button Click in Android

它允许您更改活动的主题,但它会调用finish()并重新启动活动。所以,我不确定它会对你想要的东西起作用。

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

2
投票

我知道这个问题很老,但这个问题对我来说是新的,所以它也可能对其他人有所帮助。

尝试:

  • getSupportActionBar().setElevation(0);

或者,如果您没有使用自定义操作栏:

  • getActionBar().setElevation(0);

我认为这是以编程方式执行此操作的最简单方法。


0
投票

如果supportActionBar.elevation=0f不适合你,可以通过设置appBar.targetElevation=0f来删除阴影,但不推荐使用此方法。新方法使用StateListAnimator

您必须将stateListAnimator设置为AppBarLayout,不能通过以下步骤实现

  1. 创建动画师资源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"定义了海拔。
  2. 膨胀并将StateListAnimator设置为appBar val stateAnimator=AnimatorInflater.loadStateListAnimator(this,R.animator.appbar_animator); appBar.stateListAnimator=stateAnimator

StateListAnimator被添加到api 21 Lollipop中

© www.soinside.com 2019 - 2024. All rights reserved.