Android 切换主题风格(Theme换肤效果)

参考

1、Android 切换主题以及换肤的实现

截图

1、默认打开


image.png

2、点击【换主题色】


image.png

需知

  • 主题色运用:manifest清单文件中application的属性之一,android:theme="@style/AppTheme"
  • 在style.xml中定义不同风格的theme(对app而言的style啦)
  • 熟悉?attr/colorPrimary等属性,在theme中定义好后,布局文件用到这些属性可以自动替换
  • 如何点击触发后整体画面生效

代码

1、清单文件manifest中,app默认使用AppTheme(或者自定义Theme)
<application
        ...
        android:theme="@style/AppTheme">
        ...
</application>
2、style.xml 风格:
  • 可定义app的theme,比方说AppTheme和MyTheme
  • 一般用来定义app内各种控件样式,可重复使用,减少layout中的代码量,亦可做到后期维护换色等快速修改,不用一一修改。
  • 用到一些自定义属性
<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <!--自定义属性-->
        <item name="myBgColor">@color/wx_bg_gray</item>
        <item name="myButtonHeight">60dp</item>
    </style>


    <style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/sysColorPrimary</item>
        <item name="colorPrimaryDark">@color/sysColorPrimaryDark</item>
        <item name="colorAccent">@color/sysColorAccent</item>
        <item name="actionBarSize">80dp</item>
        <item name="colorButtonNormal">@color/wx_green</item>

        <!--自定义属性-->
        <item name="myBgColor">@color/wx_sunset</item>
        <item name="myButtonHeight">100dp</item>
    </style>

    <!--button style-->
    <style name="MyBtnStyle">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">?attr/myButtonHeight</item>
    </style>

</resources>

3、attr.xml 自定义属性(一般做自定义View的人肯定熟悉):
  • 如果不用可忽略。这里定义后,就可在style.xml中使用这个item属性了,并可以写?attr/myBgColor 和 ?attr/myButtonHeight
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="myBgColor" format="color" />
    <attr name="myButtonHeight" format="dimension" />
</resources>
4、layout.xml 布局文件使用
  • 这里背景使用到:android:background="?attr/myBgColor"
  • 其中一个按钮用到:style="@style/MyBtnStyle",而这个style里面属性用到<item name="android:layout_height">?attr/myButtonHeight</item> 。 而且这个myButtonHeight定义到了2个theme里面了!!!
<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"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:background="?attr/myBgColor">

    <TextView
        android:id="@+id/sample_text"
        android:textSize="25sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        />

    <EditText
        android:id="@+id/et_1"
        android:textSize="25sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Edit it"/>

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox"
        android:checked="true"/>

    <Button
        android:id="@+id/btn_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击这里"/>

    <Button
        android:id="@+id/btn_2"
        style="@style/MyBtnStyle"
        android:text="换主题色"/>

</LinearLayout>
5、代码控制切换主题
  • setTheme(),必须在setContentView(),绘画画面之前
  • 重启当前画面:recreate()
  • 当前画面改了,但是除了栈顶的活动画面,之后新打开画面可以是新的theme,之前在栈里存活的活动画面还是不能及时换theme,这个此处不写了,太多情况了,反正可以控制的。
//切换不同的风格,必须在setContentView之前做
        if(useMyTheme){
            setTheme(R.style.MyTheme);
        }else{
            setTheme(R.style.AppTheme);
        }

        setContentView(R.layout.activity_main);


        btn_2 = findViewById(R.id.btn_2);
        btn_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                useMyTheme = !useMyTheme;
                recreate(); //重启画面
            }
        });
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,012评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,589评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,819评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,652评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,954评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,381评论 1 210
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,687评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,404评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,082评论 1 238
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,355评论 2 241
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,880评论 1 255
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,249评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,864评论 3 232
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,007评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,760评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,394评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,281评论 2 259

推荐阅读更多精彩内容