Android开发,设置状态栏颜色

【Android开发,设置状态栏颜色】五陵年少金市东,银鞍白马渡春风。这篇文章主要讲述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"

    推荐阅读