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

Joh*_*ohn 17 android styles dropshadow android-theme android-actionbar

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

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

<style name="MyTheme" parent="Theme.Sherlock">
....
<item name="windowContentOverlay">@null</item>
<item name="android:windowContentOverlay">@null</item>
....
</style>
Run Code Online (Sandbox Code Playgroud)

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

era*_*tin 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>
Run Code Online (Sandbox Code Playgroud)

现在您可以将它们设置为以下活动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"/>
Run Code Online (Sandbox Code Playgroud)

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

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

    //...
}
Run Code Online (Sandbox Code Playgroud)


ytR*_*ino 8

If you won't make more theme, then try this. This works nicely for me!

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

From Kitkat(maybe), ActionBarOverlayLayout is included in activity's view tree.
This shows actionbar shadow (I think 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 I don't know whats happen if using support library version.


Íca*_*ota 5

我知道这个问题很旧,但是这个问题对我来说是新问题,因此也可能对其他人有所帮助。

尝试:

  • getSupportActionBar().setElevation(0);

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

  • getActionBar().setElevation(0);

我认为这是通过编程实现此目的的最简单方法。


Onu*_* A. 3

声明两种样式 ne 有阴影,另一种没有。然后在清单中定义每个活动的主题。例如

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

...

<Activity 
android:theme="@style/ThemeNoShadow" ...>
Run Code Online (Sandbox Code Playgroud)