问题
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.
回答1:
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);
//...
}
回答2:
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);
}
}
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.
回答3:
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.
回答4:
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
回答5:
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" ...>
回答6:
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;
}
}
}
回答7:
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
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.Inflate and set the
StateListAnimator
toappBar
val stateAnimator=AnimatorInflater.loadStateListAnimator(this,R.animator.appbar_animator); appBar.stateListAnimator=stateAnimator
StateListAnimator was added in api 21
Lollipop
来源:https://stackoverflow.com/questions/19922078/remove-action-bar-shadow-programmatically