android中手机的导航栏变成固定颜色(只有真机才有效果)

1.在module的build.gradle文件中添加下面依赖
compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'

2.在我们界面的Activity中的setContentView(R.layout.activity_register)之上添加下面逻辑代码

//设置沉浸者1a7bd5
UIUtils.initSystemBar(this, 0xff1a7bd5);

3.在我们的界面的XML文件中的父布局中添加下面属性

android:clipToPadding="false"
android:fitsSystemWindows="true"



4.添加工具类
UIUtils.java

public class UIUtils {
/**
* String-array id 返回字符串数组
*
* @param id
* @return
*/
public static String[] getStringArray(int id) {
return getResources().getStringArray(id);
}

/**
* 获取Resources对象
*
* @return
*/
public static Resources getResources() {
return getContext().getResources();
}

/**
* 获取 Context对象
*
* @return
*/
public static Context getContext() {
return MyApp.getInstance();
}

public static View inflate(int id) {
return View.inflate(UIUtils.getContext(), id, null);
}

/**
* 根据手机的分辨率从 dip 的单位 转成为 px(像素)
*/
public static int dip2px(float dpValue) {
final float scale = UIUtils.getContext().getResources().getDisplayMetrics().density; //屏幕的密度dpi
return (int) (dpValue * scale + 0.5f);
}

/**
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
*/
public static int px2dip(float pxValue) {
final float scale = UIUtils.getContext().getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}

/**
* 设置状态栏颜色
*
* @param activity activity
* @param colorId 颜色值
*/
public static void initSystemBar(Activity activity, int colorId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window win = activity.getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
//修改window的综合属性flags
// WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS含义为状态栏透明
winParams.flags |= WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
win.setAttributes(winParams);
}
//调用开源库SystemBarTintManager进行状态栏着色 产生沉浸式效果
SystemBarTintManager tintManager = new SystemBarTintManager(activity);
tintManager.setStatusBarTintEnabled(true);//使用状态栏着色可用
tintManager.setStatusBarTintColor(colorId);//指定颜色进行着色
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值