Android 以编程方式删除操作栏阴影
如何从java代码中删除操作栏的阴影 如果我把它从样式中去掉,就可以了Android 以编程方式删除操作栏阴影,android,styles,android-actionbar,android-theme,dropshadow,Android,Styles,Android Actionbar,Android Theme,Dropshadow,如何从java代码中删除操作栏的阴影 如果我把它从样式中去掉,就可以了 <style name="MyTheme" parent="Theme.Sherlock"> .... <item name="windowContentOverlay">@null</item> <item name="android:windowContentOverlay">@null</item> .... </style> .... @空的
<style name="MyTheme" parent="Theme.Sherlock">
....
<item name="windowContentOverlay">@null</item>
<item name="android:windowContentOverlay">@null</item>
....
</style>
....
@空的
@空的
....
但是我需要从java代码中动态删除并添加它。声明两种样式ne有阴影,而另一种没有阴影。然后在清单中用各自的主题定义每个活动的主题。 e、 g
...
定义这样的新样式。请注意,没有定义父级:
<style name="ConOver" > <<== no parent
<item name="android:windowContentOverlay">@null</item>
</style>
无法通过编程方式设置
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>
或者,您可以在onCreate()
方法中以编程方式设置正确的主题:
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.MyNoActionBarShadowTheme);
super.onCreate(savedInstanceState);
//...
}
我发现这篇文章:
它允许您更改活动的主题,但它会调用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;
}
}
}
如果你不想做更多的主题,那么试试这个。这对我很管用
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);
}
}
public void disableActionbarShadow(){
if(Build.VERSION.SDK_INT
从Kitkat(可能),ActionBarOverlayLayout包含在活动的视图树中。这显示了actionbar阴影(我想是XD) 参考文献:
因为我不知道如果使用支持库版本会发生什么情况。我知道这个问题很老,但这个问题对我来说是新的,所以它可能也会帮助其他人 尝试:
李>getSupportActionBar().setElevation(0)
李>getActionBar().setElevation(0)
我认为这是最简单的编程方式。如果
supportActionBar.elevation=0f
不适合您,
通过设置appBar.TargetLevation=0f可以移除阴影,但此方法已被弃用。
新方法正在使用
您必须将stateListAnimator
设置为AppBarLayout
,而不使用可以通过以下步骤实现的提升
appbar\u 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>
StateListAnimator是在api
21
Lollipop中添加的。我可以问一下为什么要使用java删除它吗?我想要阴影,在操作栏下方的某些活动,而不是其他活动。我不知道为什么这个答案被否决,这是一种以编程方式删除actionbar阴影的方法,它可以工作。可能是因为它只在setContentView之前工作,这有点违背了以编程方式进行操作的目的。最好先在活动上有不同的风格。@格雷戈,我不知道为什么你会考虑两种活动,它们的风格不同。此外,如果您注意到,我们正在样式中添加/更新属性,而不是使用其他属性。你能指出这个页面上一个不推荐的答案吗?使用两个独立的活动| | |重新创建一个活动| |在setContentView(…)之前设置主题。@GregEnnis顺便说一句,被接受的答案(第二部分)与我的答案有相同的限制,在我的答案发布之后很久,并建议在运行时使用两个单独的主题,而不是覆盖属性。在发帖的时候,我已经惹恼了一些人,他们连续否决了我的答案。在发布的瞬间,我获得了一个-2
。任何人都不想用这个分数来尝试答案:)。@Vikram我没有投你反对票,事实上我只是投了你一票,因为getTheme()。applyStyle()是一个很好的工具,可以放在魔术袋里。当然,它在其他情况下也很有用。仅仅改变一种风格并不需要完全不同的主题定义。
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;
}
}
}
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);
}
}
<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>
val stateAnimator=AnimatorInflater.loadStateListAnimator(this,R.animator.appbar_animator);
appBar.stateListAnimator=stateAnimator