【发布时间】:2016-10-17 01:58:40
【问题描述】:
我想在targetSdkVersion 19 及以上的 Android 设备中设置状态栏颜色。对于 Lollipop (21) 及以上的版本,我使用这行代码运行良好:getWindow().setStatusBarColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimaryDark)); 将我的应用程序的状态栏颜色设置为colorPrimary。但是,我注意到,当我尝试将此 API 用于 SDK 版本 19 及以下版本时,我需要使用版本 21。结果,我查看了它并发现 How to change the status bar color in android 可以解决我的问题,但是当我尝试使用以下代码(取自上面的链接)
public static void setTaskBarColored(Activity context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
Window w = context.getWindow();
w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//status bar height
int statusBarHeight = Utilities.getStatusBarHeight(context);
View view = new View(context);
view.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
view.getLayoutParams().height = statusBarHeight;
((ViewGroup) w.getDecorView()).addView(view);
view.setBackgroundColor(context.getResources().getColor(R.color.colorPrimaryTaskBar));
}
}
状态栏颜色已设置,但它从黑色变为我希望它具有的colorPrimary。总的来说,我希望状态栏颜色全部为colorPrimary,而不是从黑色渐变为colorPrimary。对于下载了 KitKat 的 android 设备,有什么办法可以让状态栏全部为 colorPrimary 吗?还是有原因导致 android KitKat 状态栏从黑色变为 colorPrimary?任何一个答案都会有所帮助。谢谢!
【问题讨论】:
标签: android android-5.0-lollipop android-4.4-kitkat android-statusbar