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 ...
随机推荐
- hibernate分页功能
设置开始位置无效 设置终止位置是可以的 设置完之后,并没有分页 问题解决 https://blog.csdn.net/qq_39859824/article/details/77902488 http ...
- 常用长度单位PX/EM/PT/百分比转换公式与对照表
PX.PT.EM.ex和in等都是我们常用的长度单位,尤其在网页的字体大小中经常用到.但是你知道PX.PT和EM之间是如何进行准换的吗?这里icech为大家找到了一个px.pt.em和percent大 ...
- img标签下多余空白BUG解决方法
在进行页面的DIV CSS排版时,遇到IE6(当然有时Firefox下也会偶遇)浏览器中的图片元素img下出现多余空白的问题绝对是常见的 对于该问题的解决方法也是“见机行事”. 1.将图片转换为块级对 ...
- CentOS 7 yum 安装与配置MySQL5.7
1.下载mysql源安装包 wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm 2.安装mysql源 yu ...
- axios简单的二次封装
import axios from 'axios' import { Message} from 'element-ui' import store from '../store' //vuex im ...
- TP5.1 首页路由
把自带的return 删了
- 从0开始学习 GitHub 系列之「05.Git 进阶」
关于 Git 相信大家看了之前一系列的文章已经初步会使用了, 但是关于Git还有很多知识与技巧是你不知道的,今天就来给大家介绍下一些 Git 进阶的知识. 1. 用户名和邮箱 我们知道我们进行的每一次 ...
- go语言第一问:在其他地方执行编译go语言程序,结果会在哪个地方产生?
1.我们看执行编译go语言程序中命令,没有找到exe文件.
- 当inline-block和text-indent遇到IE6,IE7
在实际应用中,考虑到seo,很多button,icon都要用到inline-block和text-indent来处理,例如: <a href="#" class=" ...
- console 中的格式化打印(占位符),和样式定义
格式化打印 Gecko 9.0 (Firefox 9.0 / Thunderbird 9.0 / SeaMonkey 2.6) 首次发布对string substitutions的支持.你可以在传递给 ...