Android 主题Theme样式一键换肤,非常简单(附小案例)

导语

    谷歌v7后的主题Theme其实就有意给开发者们开辟换肤的功能,我们一起手动制作一款可以换肤主题,开始撸码吧!

一、统一自定义属性名

attr.xml

   <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <attr name="mainColor" format="color" />
        <attr name="mainPrimaryTextColor" format="color" />
        <attr name="mainPrimaryLightTextColor" format="color" />
        <attr name="mainBgColor" format="color" />
    </resources>

属性资源 attr 里可以定义属性类型:如color、float、integer、boolean、dimension(sp、dp/dip、px、pt…)、reference(指向本地资源)等等。

二、在Value文件中自定义若干套主题

style.xml

<resources>
 <!-- Base application theme. -->
    <style name="AppThemeNoAction" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="mainBgColor">#BBDEFB</item>
        <item name="mainColor">#2196F3</item>
    </style>

    <!--换肤-->
    <style name="AppThemeNoAction.Green">
        <item name="colorPrimary">#4CAF50</item>
        <item name="colorPrimaryDark">#388E3C</item>
        <item name="colorAccent">#F3F5F3</item>
        <item name="mainBgColor">#C8E6C9</item>
        <item name="mainColor">#4CAF50</item>
    </style>

    <style name="AppThemeNoAction.Blue">
        <item name="colorPrimary">#2196F3</item>
        <item name="colorPrimaryDark">#1976D2</item>
        <item name="colorAccent">#607D8B</item>
        <item name="mainBgColor">#BBDEFB</item>
        <item name="mainColor">#2196F3</item>
    </style>

    <style name="AppThemeNoAction.Yellow">
        <item name="colorPrimary">#FFC107</item>
        <item name="colorPrimaryDark">#FF9800</item>
        <item name="colorAccent">#927215</item>
        <item name="mainBgColor">#D1C4E9</item>
        <item name="mainColor">#FFC107</item>
    </style>

    <style name="AppThemeNoAction.Grey">
        <item name="colorPrimary">#607D8B</item>
        <item name="colorPrimaryDark">#455A64</item>
        <item name="colorAccent">#58575A</item>
        <item name="mainBgColor">#CFD8DC</item>
        <item name="mainColor">#607D8B</item>
    </style>
</resources>

三、指定自定义颜色

需要跟随主题切换颜色的控件,可以直接设置背景、字体颜色,字符串或图片属性赋值。

(1)xml中赋值: 

 <?xml version="1.0" encoding="utf-8"?>
    <Linerlayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/mainColor"
        android:minHeight="?attr/actionBarSize" />

(2)代码中赋值:

  TypedArray a = obtainStyledAttributes(new int[]{R.attr.mainBgColor, 
    R.attr.mainColor});
  
  int color = a.getColor(0, Color.BLACK)

//或

  TypedValue typedValue = new TypedValue();

  newTheme.resolveAttribute(mAttrResId, typedValue, true)
 

这里只列出了颜色;还可以使用引用主题字符串以及图片。

四、工具类


                
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

艾阳Blog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值