我设置了一个项目并希望使用Material设计。由于应用程序应与低于21的API兼容,我使用appcompat。 不幸的是我无法改变状态栏的颜色。
我已阅读:How to change the status bar color in android 并且:https://chris.banes.me/2014/10/17/appcompat-v21/
我的布局是android.support.v4.widget.DrawerLayout。 ActionBar的颜色会更改,但不会更改状态栏的颜色。我的xml:
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">@color/green500</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">@color/green900</item>
</style>
答案 0 :(得分:1)
实际上,应用无法使用低于API 19的状态栏颜色更改。
我写了一个演示。当API级别高于或等于API 19时,它工作正常。 你的风格设置是对的。但是,如果您希望该应用程序与API 19兼容。 你应该添加一些代码。
class Utils {
private static int getStatusBarHeight(Context context) {
Context appContext = context.getApplicationContext();
int result = 0;
int resourceId = appContext.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = appContext.getResources().getDimensionPixelSize(resourceId);
}
return result;
}
static void setStatusbarColor(Activity activity, View view) {
Window w = activity.getWindow();
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
w.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
w.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
int statusBarHeight = getStatusBarHeight(activity);
view.setLayoutParams(new CoordinatorLayout.LayoutParams(CoordinatorLayout.LayoutParams.MATCH_PARENT
, view.getLayoutParams().height + statusBarHeight));
view.setPadding(0, statusBarHeight, 0, 0);
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
Utils.setStatusbarColor(this, toolbar);
setSupportActionBar(toolbar);
}
如果你想检查所有代码。请到这里:https://github.com/SawDouma/StatusBarColorChanged/tree/master