【Android L】Material Design低版本实现之【应用主题并且修改状态栏颜色】

在安卓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
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值