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"
原文地址:https://www.cnblogs.com/buyishi/p/9571587.html
时间: 2024-10-08 03:07:15