【Android L】Material Design低版本实现之【应用主题并且修改状态栏颜色】
来源:互联网 发布:网络电影怎么发布 编辑:程序博客网 时间:2024/05/17 02:59
在安卓5.0发布以后,界面实在美到爆,状态栏的颜色也可以自定义了。于是乎我就有想法将这一特性引入到我自己的APP中。查了很多资料,为了让5.0以前版本的系统享受到material design,google自然会推出相应的兼容包:即appcompathttps://www.zybuluo.com v21,这个兼容包里面有很多有意思的东西,不是这篇文章的重点,以后再讲。在官方文档https://chris.banes.me/2014/10/17/appcompat-v21/中介绍了可以引用这个包,然后在样式中配置如下主题样式就可以达到我们的目的
下面就来讲讲如何实现改变状态栏颜色(Android Studio):
第一步:
导入appcompat-v7:21
在gradle项目中,你需要将compile sdk版本设置成21,如下:
android { compileSdkVersion 21 buildToolsVersion "21.1.2" defaultConfig { applicationId "com.example.test.materialdesign_theme" minSdkVersion 14 targetSdkVersion 21 versionCode 1 versionName "1.0" }}dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile "com.android.support:appcompat-v7:21.0.+"}
第二步:指定Material Theme主题
你需要将自己的主题继承自Theme.AppCompat,新的AppCompat有你所需要的支持Material Design的兼容代码与资源文件。在values-v21中我用自定义的base thmeme替代了原始主题,这是因为我要在里面使用5.0的各种transitions(过度)效果。
values/styles.xml
<resources> <style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Theme customizations available in newer API levels can go in res/values-vXX/styles.xml, while customizations related to backward-compatibility can go here. --> </style> <!-- Base application theme. --> <style name="AppTheme" parent="AppBaseTheme"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimary</item> <!--为控件的选中颜色,即checked或selected状态的颜色。--> <item name="colorAccent">@color/colorPrimary</item> <!--实际上为Actionbar中的文本颜色--> <item name="android:textColorPrimary">@android:color/white</item> <!-- colorControlNormal:为控件预设的颜色,相当与之前定义xml Drawable中Selector的Normal状态的颜色。 colorControlActivated:即激活状态,按照源代码仅有即几行解释应该是属于accent的一部分如checked状态(需要实际测试一下)。 colorControlHighlight:为选择高亮,如ripples,列表的selector。 coloSwitchThumbNormal:是指switch控件的色彩了。 --> <item name="android:windowNoTitle">true</item> <item name="windowActionBar">false</item> </style></resources>
values-v21/styles.xml
<?xml version="1.0" encoding="utf-8"?><resources><style name="AppTheme" parent="AppTheme.Base"> <item name="android:windowContentTransitions">true</item> <item name="android:windowAllowEnterTransitionOverlap">true</item> <item name="android:windowAllowReturnTransitionOverlap">true</item> <item name="android:windowSharedElementEnterTransition">@android:transition/move</item> <item name="android:windowSharedElementExitTransition">@android:transition/move</item></style></resources>
**注意**:Demo中使用了toolbar,所以Activity的样式必须继承于Theme.AppCompat,Activity也必须继承自 ActionBarActivity,不然会报错的。这里最好的方式是在application节点下配置默认的样式,这样配置一次就可以了。
第三步:修改布局文件
首先我们把toolbar单独创建出来,这样方便复用。如下在layout中创建toobar.xml
<?xml version="1.0" encoding="utf-8"?><merge> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimaryDark"/></merge>
接着将toobar添加到我们的布局文件中。如下activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:orientation="vertical" android:fitsSystemWindows="true" tools:context=".MainActivity"> <include layout="@layout/toolbar"></include> <TextView android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
注意:android:fitsSystemWindows,如果置为true时,作用是空出状态栏的位置,以免我们的的toolbar直接顶到屏幕的顶部。
第四步:导入支持包到工程
说明:这个支持包是我从github上的开源项目上脱下来的,就是一个java文件,方便我们自己修改。
下载地址:https://github.com/hexiaochun/SystemBarTint
第五步:修改代码
添加对toobar的支持:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); if(toolbar != null) { setSupportActionBar(toolbar); }}
添加修改状态栏颜色代码(仅对android4.4及以上才有用):
//设定状态栏的颜色,当版本大于4.4时起作用 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Window window = getWindow(); // Translucent status bar window.setFlags( WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); /*// Translucent navigation bar window.setFlags( WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);*/ SystemBarTintManager tintManager = new SystemBarTintManager(this); tintManager.setStatusBarTintEnabled(true); //此处可以重新指定状态栏颜色 tintManager.setStatusBarTintResource(R.color.colorPrimary); }
好了,大功告成。
最终效果:
0 0
- 【Android L】Material Design低版本实现之【应用主题并且修改状态栏颜色】
- 在低版本android系统上实现Material design应用
- 在低版本android系统上实现Material design应用(转载)
- Android新浪微博开发(二)界面设计之低版本实现Material Design效果
- 在低版本android系统上实现Material设计应用
- 在低版本android系统上实现Material设计应用
- 在低版本android系统上实现Material设计应用
- 在低版本android系统上实现Material设计应用
- ANDROID L - Material Design详解(主题和布局)
- ANDROID L - Material Design详解(主题和布局)
- android 配置Material Design主题(沉浸式状态栏的实现)
- Material Design 实现之主题使用Theme
- Material Design 实现之主题使用Theme
- Android 之低版本高版本实现沉浸式状态栏
- android material design 主题
- Android 应用 Material Design 颜色图解
- Android Material Design向下兼容至低版本Android SDK设备
- AppCompat 21实现低版本手机使用Material Design
- 【转载】Java学习之 多态 Polymorphism
- 非常好的理解遗传算法的例子
- POJ 3461--Oulipo【KMP】
- Android注册短信验证码功能
- 教程学习笔记 Digital Tutors – Utilizing Advanced Shading Techniques Using V-Ray in Maya
- 【Android L】Material Design低版本实现之【应用主题并且修改状态栏颜色】
- 华为练习 对象管理器
- Sharepoint 2013 管理APP store(打开,禁用和只有审批后才能够买)
- 黑马程序员——C语言学习之流程结构-if—switch—while—do while
- Java中的静态方法和静态变量
- final、finally、finalize的区别
- 数字三角形
- ubuntu14.4---内核树建立
- spring aop 学习