Android开发,设置状态栏颜色
Posted buyishi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android开发,设置状态栏颜色相关的知识,希望对你有一定的参考价值。
StatusBarColorSetter.java
package com.example.buyishi.translucentdemo; import android.app.Activity; import android.content.res.Resources; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; public class StatusBarColorSetter { public static void setStatusBarColor(Activity activity, int color) { Window window = activity.getWindow(); window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); ViewGroup decorView = (ViewGroup) window.getDecorView(); View statusBarView = new View(activity); Resources resources = activity.getResources(); int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android"); int statusBarHeight = resources.getDimensionPixelSize(resourceId); ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight); statusBarView.setLayoutParams(layoutParams); statusBarView.setBackgroundColor(color); decorView.addView(statusBarView); } }
为避免界面布局向上偏移,须在布局根元素中添加android:fitsSystemWindows="true"
以上是关于Android开发,设置状态栏颜色的主要内容,如果未能解决你的问题,请参考以下文章