码迷,mamicode.com
首页 > 移动开发 > 详细

Android开发,设置状态栏颜色

时间:2018-09-01 23:56:57      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:tde   itss   new   flags   parent   group   you   manage   r.java   

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开发,设置状态栏颜色

标签:tde   itss   new   flags   parent   group   you   manage   r.java   

原文地址:https://www.cnblogs.com/buyishi/p/9571587.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!