Android 使用SystemBarTint设置状态栏颜色
做项目时,发现APP的状态栏是系统默认的颜色,突然想到,为啥别的APP是自己设置的颜色(和APP本身很相搭),于是也想给自己的APP设置系统状态栏的颜色,更加美美哒。。。
搜了下,发现原来设置状态栏居然有个很高大上的名字(听不懂的都是高大上)——沉浸式状态栏,Android4.4以后开始支持沉浸式状态栏, 继续搜索,发现,有一个很简单的开源项目——SystemBarTint,可以很完美的支持沉浸式状态栏。
SystemBarTint地址: https://github.com/hexiaochun/SystemBarTint
下面,简单演示下如何使用该库,首先,先看下效果,有图才有真相:
1. 引入类库
使用Android Studio,直接在build.gradle文件中引入库:
dependencies {
compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
}
使用Eclipse,可下载JAR包,并引入到项目的libs文件夹中。
2. 在Activity中添加方法:
/**
* Apply KitKat specific translucency.
*/
private void applyKitKatTranslucency() { // KitKat translucent navigation/status bar.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
SystemBarTintManager mTintManager = new SystemBarTintManager(this);
mTintManager.setStatusBarTintEnabled(true); mTintManager.setStatusBarTintResource(R.color.colorTop);//通知栏所需颜色
} } @TargetApi(19)
private void setTranslucentStatus(boolean on) {
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
if (on) {
winParams.flags |= bits;
} else {
winParams.flags &= ~bits;
}
win.setAttributes(winParams);
}
然后, 在OnCreate()方法中调用applyKitKatTranslucency方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); applyKitKatTranslucency();
}
3. 在style.xml中,添加系统的样式:
<!-- 去掉tab顶部的黑边 -->
<style name="no_title" parent="@android:style/Theme.Light.NoTitleBar"> <!-- 沉浸式状态栏 -->
<item name="android:fitsSystemWindows">true</item>
<item name="android:clipToPadding">false</item>
</style>
当然了,别忘了在AndroidManifest.xml进行配置主题:
<application
android:name=".activity.base.MyApp"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:persistent="true"
android:theme="@style/no_title">
</application>
注: 这个是必要的,如果不添加,会造成一些页面的变形。
综上, 便可以在4.4以上的系统中方便的设置状态栏颜色,有木有感觉你的APP变得更好看了呢!
扩展:http://www.jianshu.com/p/e1c937000343
Android 使用SystemBarTint设置状态栏颜色的更多相关文章
- Android中设置状态栏颜色和字体颜色
1.在这里设置的状态栏背景为白色,字体为暗色 创建一个方法进行设置: protected void setStatusBar() { if (Build.VERSION.SDK_INT >= B ...
- android 一个TextView设置多种颜色
时候一个文本框为了强调内容需要显示不同颜色,用以下代码可以轻松实现 方法一:(适用于颜色变化多的情况) //为文本框设置多种颜色 textView=(TextView)findViewById(R ...
- Android 针对单个Activity设置状态栏颜色
代码如下: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//因为不是所有的系统都可以设置颜色的,在4.4以下就不可以. ...
- Android设置状态栏颜色
1.代码设置if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Window window = this.getWindow ...
- 使用 preferredStatusBarStyle 设置状态栏颜色
iOS9之前,在plist文件中 插入一个新的key,名字为View controller-based status bar appearance,并将其值设置为NO. 然后敲入代码: [UIAppl ...
- iOS之设置状态栏颜色
状态栏的字体为黑色:UIStatusBarStyleDefault 状态栏的字体为白色:UIStatusBarStyleLightContent 一.在info.plist中,将View contro ...
- Android Studio主题设置、颜色背景配置
打开http://color-themes.com/有很多样式可供选择 导入方式 下载主题—xxx.jar 注意:如果我们下载下来的jar名字如果有空格,一定要把空格去掉,同时文件路径中不要含有中文 ...
- Android开发技巧——设置系统状态栏颜色
开门见山,先来三张效果图: 然后我们再来讲如何实现以及如何快速地实现. 如何实现 实现设置系统状态栏颜色需要至少在Android 4.4.2(API 19)以上.这是因为,在这个版本以下,没有任何的A ...
- android 设置状态栏与标题背景颜色一致
必须在Android4.4以上版本才能设置状态栏颜色: 一.在单个Activity里面,设置状态栏的背景: 效果: 1.在Activity的布局根文件中添加属性: android:fitsSystem ...
随机推荐
- nfs网络共享服务基础
nfs原理 1.开启RPC服务 2.NFS向RPC服务注册启动的端口 3.用户向RPC询问NFS服务的端口 4.RPC返回端口给客户端 5.客户端通过获得的端口与NFS服务器进行数据传输 实验步骤 一 ...
- Spring boot随时获取ApplicationContex
@Service public class SpringManager implements ApplicationListener<ContextRefreshedEvent> { pr ...
- 第八章—BOM(一)
ECMAscript是JS的核心,而要在web上使用JS,那么BOM无疑是真正的核心.BOM叫浏览器对象模型,它提供了许多对象,用于访问浏览器的功能. BOM的核心对象是window,它表示浏览器的一 ...
- LintCode刷题笔记-- BackpackIV
标签: 动态规划 描述: Given an integer array nums with all positive numbers and no duplicates, find the numbe ...
- Leetcode700.Search in a Binary Search Tree二叉搜索树中的搜索
给定二叉搜索树(BST)的根节点和一个值. 你需要在BST中找到节点值等于给定值的节点. 返回以该节点为根的子树. 如果节点不存在,则返回 NULL. class Solution { public: ...
- NFS实现(双httpd + php-fpm + nfs + mysql 搭建discuz论坛)的方法
NFS相关介绍 一.NFS简介 1. NFS(Network File System):NFS是一个文件共享协议, 也是是在类Unix系统中在内核中实现的文件系统. 2. 起源:最早是由SUN公司研发 ...
- laravel学习文档
https://github.com/barryvdh/laravel-debugbar Laravel 精选资源大全 http://laravelacademy.org/post/153.html ...
- socket in android
Server - JAVA package com.jim.ndkdemo; import android.net.LocalServerSocket; import android.net.Lo ...
- php表单传值--GET和POST
一. 传值 1. 传值/接收方法: 1) GET(5种方式!) a) 表单Form: method = ‘get’ GET接收数据方式: b) ...
- could not insert: [com.trs.om.bean.UserLog] The user specified as a definer ('root'@'127.0.0.1') does not exist
2019-07-01 11:24:09,315 [http-8080-24] org.hibernate.util.JDBCExceptionReporter logExceptionsWARN: S ...