Android style & Theme 再探析(三)

定制Theme示例和踩坑大汇总

基于前两篇的文章的探究,本次带来的是皮肤切换和日夜间模式切换,以及基于重写部分控件展示Theme的作用;以及对于实际使用时的讲解,踩过的一些坑的汇总

Android Theme切换主题总结

笔者本文讲述在实践换肤以及日夜间模式切换的简单Demo

Android Demo展示讲解

效果展示

Demo效果展示

由图可以看到,是本示例由多种Theme和夜间模式配合展示效果,并且针对已有的activity也进行变换,这个也是我目前看到的很多已有的博客demo缺失部分,针对已经存在的acitivity如何处理

代码思路展示

  1. 首先是针对style中的Theme的设置:
 <!-- Base application theme. -->
    <style name="ehiTheme" 
    parent="@style/Theme.AppCompat.DayNight.NoActionBar">
    <!-- a.将普通的Theme .AppCompat.Light 替换 .AppCompat.DayNight 用于日夜间模式替换 -->
        
        <item name="buttonStyle">@style/ehiButton</item>
         <!-- b.重写部分控件的样式,修改默认展示的效果 -->
        
        <item name="checkboxStyle">@style/ehiCheckBox</item>
        <!--两者为不同维度 内部按钮 文字等 alertDialogStyle定义内部按钮 定义内部window类似 back等属性 alertDialogTheme-->
        <item name="alertDialogTheme">@style/AppTheme.blue.AlertDialog</item>
        <item name="radioButtonStyle">@style/ehiRadioButton</item>
    </style>

    <!-- c.重写按钮样式示例展示 -->
    <style name="ehiButton" parent="@style/Base.Widget.AppCompat.Button">
        <item name="android:background">?colorAccent</item>
        <item name="android:textColor">@color/white</item>
        <item name="android:textAppearance">@style/Base.TextAppearance.AppCompat.Small</item>
    </style>
  1. 定义多种Theme
     
       <style name="ehiTheme" 
    parent="@style/Theme.AppCompat.DayNight.NoActionBar">
        <!--部分代码省略-->
    </style> 
    <style name="AppTheme" parent="ehiTheme">
        <!-- Customize your theme here. -->
         <!-- a.定义多种主题Theme前不直接当做父类,而是再次继承一遍,方便在value -v21的更高文件夹中直接利用该 AppTheme 添加新特性,而上面ehiTheme可以统一添加一些共同具有的属性 -->
    </style> 
    <!--b.定义蓝色主题-->
     <style name="AppTheme.blue">
        <item name="colorPrimary">@color/colorPrimaryBlue</item>
        <item name="colorPrimaryDark">@color/colorPrimaryBlueDark</item>
        <item name="colorAccent">@color/colorPrimaryBlueDark</item>
        <item name="alertDialogTheme">@style/AppTheme.blue.AlertDialog</item>
    </style>
     <!--c.定义紫色主题-->
     <style name="AppTheme.Purple" >
        <item name="colorPrimary">@color/colorPrimaryPurple</item>
        <item name="colorPrimaryDark">@color/colorPrimaryPurpleDark</item>
        <item name="colorAccent">@color/colorPrimaryPurpleDark</item>
        <item name="alertDialogTheme">@style/AppTheme.Purple.AlertDialog</item>
    </style>

     <application
        android:allowBackup="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:name=".DemoApplication"
        android:theme="@style/AppTheme.blue"></style>
        <!--application中引用其中一个-->
  1. 针对日夜间模式的资源文件做的调整
  • 原有的value文件夹外,再创建values-night

  • 创建两份color文件,替换为不同的颜色(ps:需要在夜间模式替换的颜色定义到night文件中即可,其他颜色保留在默认文件夹中即可)

value 文件中的color


      <color name="colorAccent">@color/colorPrimaryBlueDark</color>
    <color name="colorPrimary">@color/colorPrimaryBlue</color>
    <color name="colorBackgroundTrans">#AAFAFAFA</color>

    <color name="colorPrimaryBlue">#bbdefb</color>
    <color name="colorPrimaryBlueDark">#90caf9</color>

     <color name="colorPrimaryPurple">#e1bee7</color>
    <color name="colorPrimaryPurpleDark">#ce93d8</color>

value-night 文件中的color


      <color name="colorAccent">@color/colorPrimaryBlueDark</color>
    <color name="colorPrimary">@color/colorPrimaryBlue</color>
    <color name="colorBackgroundTrans">#AAFAFAFA</color>

   <color name="colorPrimaryBlue">#82b1ff</color>
    <color name="colorPrimaryBlueDark">#2962ff</color>

    <color name="colorPrimaryPurple">#d500f9</color>
    <color name="colorPrimaryPurpleDark">#aa00ff</color>
  1. 代码中使用多Theme和日夜间模式
  • 设定缓存Theme变化后的设置的保存位置
    本demo中使用application做为保存,真实项目请使用xml等持久化方式来保存用户设置
public class DemoApplication extends Application {
    private static DemoApplication demoApplication;
   private static final int defaultStyle = R.style.AppTheme_blue;//默认的主题
   private static int tempNightMode = AppCompatDelegate.MODE_NIGHT_NO;//默认日间模式
    private static int style = defaultStyle;
    @Override
    public void onCreate() {
        super.onCreate();
        demoApplication = this;
    }

    //getter 和 setter 方法省略

}

  • 设置界面的布局文件
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
    tools:context=".MDActivity">

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="测试按钮" />
    <RadioGroup
        android:id="@+id/rg_theme"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
          <RadioButton
            android:id="@+id/pink"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="粉色" />
        <RadioButton
            android:id="@+id/blue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="蓝色" />
        <RadioButton
            android:id="@+id/Purple"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="紫色" />

    </RadioGroup>

    <Switch
        android:id="@+id/switch_day_night"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="日夜间模式切换"
        android:checked="false"
        />
    
</LinearLayout>
  • 设置界面java代码
public class MDActivity extends AppCompatActivity {
     //控件定义   
    private RadioGroup rg;
    private Switch switchDayNight;
    private RadioButton pink;
    private RadioButton blue;
    private RadioButton Purple;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //activiy重建后,读取设置后的style
        setTheme(DemoApplication.getStyle());
        super.onCreate(savedInstanceState);
        //activiy重建后,设置界面读取日夜间模式
        if (DemoApplication.getTempNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        } else {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        }
        setContentView(R.layout.activity_md);
    initView();
    }
    //初始化视图
     private void initView() {
         //activiy重建后,设置switch开关
        switchDayNight = findViewById(R.id.switch_day_night);
        if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
            switchDayNight.setChecked(true);
        } else {
            switchDayNight.setChecked(false);
        }
        //社会
        switchDayNight.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    DemoApplication.setTempNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                    Intent intent = getIntent();
                    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                    setResult(RESULT_OK);
                    finish();
                    startActivity(intent);
                    overridePendingTransition(0, 0);
                } else {
                    DemoApplication.setTempNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                    Intent intent = getIntent();
                    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                    setResult(RESULT_OK);
                    finish();
                    startActivity(intent);
                    overridePendingTransition(0, 0);
                }
            }
        });
        pink = findViewById(R.id.pink);
        blue = findViewById(R.id.blue);
        Purple = findViewById(R.id.Purple);
        rg = findViewById(R.id.rg_theme);
        setCheck();
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.pink:
                        setStyle(R.style.AppTheme_Pink);
                        break;
                    case R.id.blue:
                        setStyle(R.style.AppTheme_blue);
                        break;
                    case R.id.Purple:
                        setStyle(R.style.AppTheme_Purple);
                        break;
                    default:
                        setStyle(R.style.AppTheme_blue);
                }


            }
        });
    }
    //设置对应的主题
    private void setCheck() {

        switch (DemoApplication.getStyle()){
            case R.style.AppTheme_Pink:
                pink.setChecked(true);
                break;
            case R.style.AppTheme_blue:
                blue.setChecked(true);
                break;
            case R.style.AppTheme_Purple:
                Purple.setChecked(true);
                break;
            default:
                blue.setChecked(true);
        }
    }
   //将style进行设置,将界面进行重启
    private void setStyle(int appTheme) {
        //相同主题,不进行重启activity
        if (DemoApplication.getStyle() == appTheme) {
            return;
        }
        //缓存用户的主题设置
        DemoApplication.setStyle(appTheme);
        Intent intent = getIntent();
        //设置将activity的转场动画关闭
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        //重启时Result丢失,返回上一个界面
        setResult(RESULT_OK);
        finish();
        startActivity(intent);
        //关闭转场动画
        overridePendingTransition(0, 0);
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            setResult(RESULT_OK);//用于通知上一个界面,重启
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


  • 进入设置界面前的界面
public class MainActivity extends AppCompatActivity {
    //设置 setting动作
    private int SETTINGS_ACTION = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //读取新的style
        setTheme(DemoApplication.getStyle());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
   //用于重新创建已有界面,来展示新的style和日夜间模式
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SETTINGS_ACTION && resultCode == RESULT_OK){
            finish();
            Intent intent = getIntent();
            startActivity(intent);
        }
    }
}
  1. 实现思路

主要的实现思路非常简单:

a. 其实就是由用户选定对应的style和日夜间模式后,关闭动画的情况下,重新启动 设置的activity

b. 返回上一界面时,界面style或日夜间模式有改动就在设置界面

setResult(RESULT_OK);

然后在上一个界面获取,然后重启界面即可

if (requestCode == SETTINGS_ACTION && resultCode == RESULT_OK){
            finish();
            Intent intent = getIntent();
            startActivity(intent);
        }
  1. 总结

总的来说其实,切换theme主题本身并没有涉及什么很高深的技术,只是对于我们所了解的基础的内容进行一个组合,我们可以用这些很简单知识创建一个更好的用户体验!

  1. 注意点
  • theme当中不要直接引用

    <style name="ehiTheme" 
      parent="@style/Theme.AppCompat.DayNight.NoActionBar">
    

    而是应该再次继承一次

 <style name="AppTheme" parent="ehiTheme">

这样在 ehiTheme用于管理所有android版本下的默认样式,
而AppTheme用于在多个value下定义一些版本的新特性属性

  • 日夜间模式和主题的切换针对已经活着的activity是不起作用的,本文采用的思路是直接,无动画重新创建activity,此种方式的劣势 就是需要对app的一些状态进行保存,保证重新创建后用户编辑过的内容不会丢失

  • 夜间模式的 color,style之类的只定义需要变化的部分的颜色或者样式,不用把其他日夜间都用的统一样式复制两份,避免不必要的维护成本

专题系列:
Android style & Theme 再探析(一)—— 你真的懂Style和Theme吗?
Android style & Theme 再探析(二) —— 一统View规范的大杀器——material design
Android style & Theme 再探析(三) —— 定制Theme示例和日夜间模式踩坑大汇总
Android style & Theme 再探析(四) —— 业务实践心得总结
博主博客:
http://www.whdreamblog.cn/

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

推荐阅读更多精彩内容