作者:手机用户2502854043 | 来源:互联网 | 2023-10-11 10:20
最火Android开源项目SystemBarTint使用开源地址:https:github.comopen-androidSystemBarTint修改状态栏颜色,可保持与acti
最火Android开源项目SystemBarTint使用
开源地址:https://github.com/open-android/SystemBarTint
修改状态栏颜色,可保持与actionbar 一致
PS:如果觉得这个系列的每篇文章太长,那就直接看视频吧
运行效果
view2.jpg
view3.jpg
- 爱生活,爱学习,更爱做代码的搬运工,分类查找更方便请下载黑马助手app
黑马助手.png
使用步骤
1. 在project的build.gradle添加如下代码(如下图)
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
2. 在Module的build.gradle添加依赖
compile 'com.github.open-android:SystemBarTint:v1.0.0'
3. 复制如下代码到xml
android:layout_
android:layout_
android:fitsSystemWindows="true">
android:padding="16dp"
android:textSize="18sp"
android:layout_
android:layout_
android:text="@string/ipsum"/>
4. 拷贝以下内容到 res/string.xml中
也可以自己定义自己的字符串。
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam ligula sapien, fermentum vitae aliquam sed, lacinia in nibh. Pellentesque euismod diam eget justo interdum, sit amet elementum ipsum vulputate. Aliquam sed purus urna. Vivamus luctus nisi sem, a fringilla velit iaculis id. Donec nec vulputate justo. Fusce vulputate sodales tellus, in fringilla ante posuere non. Vivamus vulputate nisl non augue dignissim, consectetur rutrum risus facilisis. Sed consectetur aliquam dolor, sed pulvinar mi tristique vitae. Donec quis ligula quis velit consequat dictum id eu felis. Mauris ac congue ante, sed elementum eros.\n\nSed eu euismod dolor, a vestibulum lorem. Phasellus euismod volutpat risus sit amet pellentesque. Proin sed massa eget risus malesuada dignissim et in ipsum..\n\nNullam risus felis, dictum et varius eget, rutrum non nunc. Vestibulum lorem nulla, porttitor ac sapien interdum, porta tristique turpis. Quisque ut dui vitae urna congue scelerisque. Nulla eu commodo felis. Nulla aliquam magna a arcu elementum, vitae tincidunt risus semper. Sed consectetur diam vel magna mattis, sed congue nisi fringilla. Ut in facilisis elit. Etiam aliquet orci urna. Nam interdum nunc fringilla iaculis cursus.
5. 拷贝以下内容到 res/color.xml中
也可以自己定义颜色。
#FF0099EE
#FF0099cc
6. 拷贝以下内容到res/style.xml 中
7. 拷贝以下内容到activity中
注意 : 应该继承的是Activity ,而不是AppCompatActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
}
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
//设置状态栏颜色
tintManager.setStatusBarTintResource(R.color.statusbar_bg);
}
@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);
}
8 . 注册activity的时候,请指定主题样式为之前定义的主题
android:theme="@style/ActionBarTheme">