Android中的主题样式

Android资源打包
这里写图片描述

资源ID
这里写图片描述
package:分为系统引用和外部引用
type:例如anim,drawable
entry:为资源的具体Id

Android主题样式

style

    统一的效果可以统一成style
    style称为样式,样式是指定一个view或者一个window的外观和格式的一组属性值

theme

    theme就是style
    theme的作用范围整个页面或整个工程
    页面中或工程中的相同控件都使用相同的style,除非指定了特定的style

定义style

    在res/values下定下xml文件
    xml的根节点必须为resources
    定义style节点,需要有name属性,可以有parent属性

示例代码

    <TextView
        android:id="@+id/text_view1"
        style="@style/TextCustomStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:text="TextView1"/>
<resources>
    <style name="TextCustomStyle" >
        <item name="android:singleLine">true</item>
        <item name="android:textColor">@color/colorBlack</item>
        <item name="android:textSize">24sp</item>
        <item name="android:textStyle">bold</item>
        <item name="android:absListViewStyle">@style/Widget.AppCompat.ListView</item>
    </style>

</resources>

继承已有的style

    1、指定parent属性
    2、前缀模式(必须为自定义的style)
<!--指定parent属性-->
    <style name="TextCustomStyle" parent="TextAppearance.AppCompat">
        <item name="android:singleLine">true</item>
        <item name="android:textColor">@color/colorBlack</item>
        <item name="android:textSize">24sp</item>
        <item name="android:textStyle">bold</item>
        <item name="android:absListViewStyle">@style/Widget.AppCompat.ListView</item>
    </style>
<!--前缀模式-->
<resources>
    <style name="TextCustomStyle.Normal">
        <item name="android:textStyle">normal</item>
    </style>
</resources>

可以使用的属性

    1、针对View
        可以查询对应View可以支持的属性
        不属于该View的属性,不会生效
    2、针对这个页面或工程
        可以查询attr
        window开始的属性都在theme

style应用

这里写图片描述

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:theme="@style/AppThemeDark">

常用的Theme

    4.0
        Holo.Dark
        Holo.Light
        Holo.Light.DarkActionBar
    5.0 
        Material.Dark
        Material.Light
        Material.Light.DartActionBar
    <attr name="gbg" format="color|reference"></attr>

    <style name="HoloDark" parent="android:Theme.Holo">

    </style>

    <style name="HoloLight" parent="android:Theme.Holo.Light">

    </style>

    <style name="HoloLightDark" parent="android:Theme.Holo.Light.DarkActionBar">

    </style>

    <style name="MaterialDark" parent="android:Theme.Holo"></style>

    <style name="MaterialLight" parent="android:Theme.Holo.Light"></style>

    <style name="MaterialLightDark" parent="android:Theme.Holo.Light.DarkActionBar"></style>
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="MaterialDark" parent="android:Theme.Material"></style>

    <style name="MaterialLight" parent="android:Theme.Material.Light"></style>

    <style name="MaterialLightDark" parent="android:Theme.Material.Light.DarkActionBar"></style>
</resources>

注:@查找引用的是当前项目,?查找并引用的当前主题的属性

style版本兼容

    1、在values/styles下声明一个default theme
    2、在values-v11下声明支持api11及以上版本的theme
    3、在values-v14下声明支持api14及以上版本的theme
    4、在values-v21下声明支持api21及以上版本的theme

support中对应的主题(已做版本兼容)

    theme.holo.light->theme.appcompat.light
    theme.holo.light.DarkaAtionBar->theme.appcompat.light.DarkActionBar
    theme.Material.Light->Theme.AppCompat.Light
    Theme.Material.Light.DarkActionBar->Theme.AppCompat.Light.DarkActionBar

注:要引入support包

compile 'com.android.support:appcompat-v7:23.4.0'

View的style查找顺序,从下到上。
这里写图片描述

动态切换主题

    1、在valus/styles 声明attr属性
    2、定义不同主体的theme,theme中给attr赋值
    3、页面加载时,设置主体
    4、无缝切换applyStyle
    <style name="AppTheme1" parent="Theme.AppCompat.Light">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="gbg">@color/colorAccent</item>
    </style>

    <style name="AppThemeDark" parent="Theme.AppCompat">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="gbg">@color/colorPrimary</item>
    </style>

    <attr name="gbg" format="color|reference"></attr>
    //主题切换
    private void styleChange() {
        //重新添加主题,true表示强制覆盖
        getTheme().applyStyle(R.style.AppTheme1, true);
        int bgColor = getBgColor();
        if (bgColor != 0) {
            root.setBackgroundColor(bgColor);
        }
    }
    //更改背景颜色
    private int getBgColor() {
        //获取主题属性
        TypedArray typedArray = getTheme().obtainStyledAttributes(new int[]{R.attr.gbg});
        int color = typedArray.getColor(0, 0);
        typedArray.recycle();
        return color;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值