Android:主题切换

一.概述

正在开发的应用做了一版新UI,原打算将新版UI按项目名做成资源包,再在build.gradle里productFlavors{ }多渠道打包实现

但被告知新旧两个项目共用一个分支,那就做成两个主题(Theme1/Theme2)来适配了

如果只是变更UI,做成多主题来适配不同项目也是比较合适的方式

一.Android App实现多主题的主要有如下几种:

  1. 从服务器下载或提前放置不同主题资源包到设备指定目录,在代码里引用主题资源
  2. 定义不同的<style>、使用Android的setTheme()机制设置不同主题
  3. 将主题资源做成APK,使用远程Context的方式访问Apk中的主题资源。

第1、第3种方法的实现原理,是用户从服务器下载 或 提前放置主题资源,然后在代码中进行引用

第2种方法是接下来先要讲的, 也是这次新旧UI开发适配完成的方式。

第2种讲完后,也会同步介绍第1、第3种方法

除了这三种常用的方式外,还有一种通过"修改framework中Resources类获取资源的流程,将资源重定向到主题包中"的实现方式,这种方式暂不做讨论

二.setTheme()实现主题切换

2.1 定义属性(attr)

App的UI由各种布局文件实现,布局文件中要定义Android各类UI属性(drawable、colors、mipmap、dimension、string......)引用UI资源。

先将Theme1、Theme2各布局文件中都有定义但需要引用不同值的UI属性挑出来,按照不同类型在/values/attrs.xml中进行定义:

<?xml version="1.0" encoding="utf-8"?>  
<resources>  
    <attr name="ColorAttrName" format="color" />  
    <attr name="integerAttrName" format="integer" />  
    <attr name="booleanAttrName" format="boolean" />  
    <attr name="dimensionAttrName" format="dimension" />  
    <item name="floatAttrName" format="dimension" />  
    <attr name="stringAttrName" format="string" />  
    <attr name="referenceAttrName" format="reference" />  
</resources>  

reference主要对应drawable、mipmap资源,其他的看字面意思

2.2 定义主题(Theme1/Theme2)

/values/style.xml中定义Theme1、Theme2

<style name="ThemeBlack" parent="@android:style/Theme.Black">  
    <item name="colorAttrName">#FF00FF00</item>  
    <item name="integerAttrName">33</item>  
    <item name="booleanAttrName">true</item>  
    <item name="dimensionAttrName">76dp</item>
    <item name="floatAttrName">0.35</item>    
    <item name="stringAttrName">@string/hello_world</item>  
    <item name="referenceAttrName">@drawable/hand</item>  
</style>

<style name="ThemeLight" parent="@android:style/Theme.Light">  
    <item name="colorAttrName">#FFFFFF00</item>  
    <item name="integerValue">55</item>  
    <item name="booleanAttrName">false</item>  
    <item name="dimensionValue">76px</item>  
    <item name="floatAttrName">0.15</item>    
    <item name="stringAttrName">@string/action_settings</item>  
    <item name="referenceAttrName">@drawable/ic_launcher</item>  
</style>

各属性的具体值可以在Theme定义时配置,但是最好定义在各自的xml配置文件中(color.xml、string.xml、dimens.xml.....),然后在Theme定义时"@类型/..."格式引用

Theme中如果string类型不是引用而是字符串,在布局文件中使用正常,但代码里获取会有问题

2.3 布局文件中引用属性

使用过程也很简单,原本布局文件中引用UI属性的方式如下:

android:color="@color/colorAttrName"
android:duration="@integer/integerValue"
android:adjustViewBounds="@bool/booleanAttrName" 
android:layout_height="@dimen/dimensionValue"
android:text="@string/stringAttrName"
android:background="@drawable/referenceAttrName"

现在改为这样进行引用:

android:color="?attr/colorAttrName"
android:duration="?attr/integerValue"
android:adjustViewBounds="?attr/booleanAttrName" 
android:layout_height="?attr/dimensionValue"
android:text="?attr/stringAttrName"
android:background="?attr/referenceAttrName"

float比较特殊,一般它会在代码中被引用(后面会讲到),配置文件中多在dimens.xml中定义变量

2.4 代码中切换主题

要配置的都配置好了,需要注意的是:

  • 最好写一个BaseActivity,其他的Activitiy都继承至它
  • 主题设置需要在setContentView()之前

接下来就在代码中进行主题切换:

package com.android.example.ui;

import android.os.Bundle;

import com.android.example.R;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class BaseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (setThemeBlack) {
            setTheme(R.style.ThemeBlack);
        } else {
            setTheme(R.style.ThemeLight);
        }
    }
}

主题设置写在BaseActivity的onCreate()函数中,它会在其他所有继承至它的Activity的onCreate()之前运行。

2.5 代码中引用属性

如果要在代码中引用 integerValue、floatAttrName、booleanAttrName,使用Context.obtainStyledAttributes()就可以了

package com.android.example.ui;

import android.os.Bundle;

import com.android.example.R;

import android.content.res.TypedArray;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class BaseActivity extends AppCompatActivity {


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (setThemeBlack) {
            setTheme(R.style.ThemeBlack);
        } else {
            setTheme(R.style.ThemeLight);
        }

        TypedArray tyar = obtainStyledAttributes(new int[]{
                R.attr.colorValue,
                R.attr.integerValue,
                R.attr.booleanValue,
                R.attr.dimensionValue,
                R.attr.floatValue,
                R.attr.stringValue,
                R.attr.referenceValue
        });

        int colorArrtValue = tyar.getColor(0, Color.BLACK);
        int integerArrtValue = tyar.getInt(1, Integer.MAX_VALUE);
        boolean booleanArrtValue = tyar.getBoolean(2, false);
        float dimensionArrtValue = tyar.getDimensionPixelSize(3, 66);
        float floatArrtValue = tyar.getFloat(4, 99.99f);
        String stringArrtValue = tyar.getString(5);
        Drawable drawableArrtValue = tyar.getDrawable(6);
    }
}

到此,通过setTheme()切换主题方式就讲述完毕了 

这种切换主题方式的优点是:不需要修改太多代码,大部分修改只要针对布局文件就行。

弊端就是:主题只能内置配置好,一旦程序发布后,应用支持的主题类型就固定了,要想增加更多主题,只能发布新的版本。

如果想要动态配置主题,可以使用前文中提到过第1、第3种主题切换方式。

就是接下来要讲的内容了

三.主题资源打包成Apk进行切换

Apk主题切换和第1种将主题包整体下载或提前放置的方案很像。

共同点是:都需要在代码中动态设置图片、文字、颜色等UI信息。

区别是:第1种方法需要自行编写资源解析的方法。而Apk主题切换,可以使用Android自带Api

APK主题方案的基本思路是:

在Android中,所有的资源都是基于包的。资源以id进行标识,在同一个应用中,每个资源都有唯一标识。但在不同的应用中,可以有相同的id。

因此,只要获取到了其他应用的Context对象,就可以通过它的 getRsources() 获取到其绑定的资源对象。

然后,就可以使用 Resources 的 getXXX 方法获取字符串、颜色、dimension、图片等。

要想获取其他应用的Context对象,Android已经为我们提供好了接口:

android.content.ContextWrapper.createPackageContext(String packageName, int flags)

package com.android.example.ui;

import android.os.Bundle;
import androidx.annotation.Nullable;

public class MainActivity extends BaseActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView text = (TextView) findViewById(R.id.remoteText);
        TextView color = (TextView) findViewById(R.id.remoteColor);
        ImageView image = (ImageView) findViewById(R.id.remote_image);

        try {
            //主题资源Apk包名
            String remotePackage = "com.xxx.themepackage";
            //主题资源Apk Context
            Context remoteContext = createPackageContext(remotePackage,CONTEXT_IGNORE_SECURITY);
            //主题资源Apk Resources
            Resources remoteResources = remoteContext.getResources();

            text.setText(remoteResources.getText(remoteResources.getIdentifier("application_name", "string", remotePackage)));
            color.setTextColor(remoteResources.getColor(remoteResources.getIdentifier("delete_target_hover_tint", "color", remotePackage)));
            image.setImageDrawable(remoteResources.getDrawable(remoteResources.getIdentifier("ic_launcher_home", "drawable", remotePackage)));
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
    }
}

四.综述

3.高级应用

内置主题实现简单、配置方便,但可扩展性不强。

Apk切换主题扩展性高,但要在代码里设置所有可变资源,一旦界面布局改变,需要较长时间进行代码改写。

实际运用中,可以将以上两种结合起来使用:

涉及到界面风格,如整体色调、不同的文字颜色,使用内置主题。图片资源则在APK主题包中提供。

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android日间模式切换是指在Android设备中切换应用程序的主题模式,以更好地适应当前环境或用户的个人喜好。在日间模式下,应用程序通常使用浅色的背景和黑色的文本,以提供清晰明亮的外观。下面是关于Android日间模式切换的一些信息: 1. 如何切换日间模式:Android设备通常提供了一个系统菜单或设置选项,允许用户切换日间模式。用户可以在“设置”或“显示”菜单中找到“主题”或“外观”选项,并选择日间模式以启用它。 2. 日间模式的用途:日间模式在白天或明亮的环境下使用,可提供更好的可读性和可见性。由于浅色背景和黑色文本的对比度较高,阅读和浏览应用程序内容会更加舒适和方便。 3. 切换日间模式的优势:切换到日间模式可以节省设备的电池寿命,因为设备的背光需要较少的功耗。同时,对于部分用户来说,浅色背景和黑色文本也可能更易于处理眼睛疲劳问题。 4. 日夜模式的应用场景:日间模式适用于大多数日常使用情况,特别是在户外环境或光线充足的环境下。例如,用户在户外使用应用程序时,日间模式可以保证内容清晰可见。 5. 自动切换和自定义设置:一些Android设备允许用户根据日出和日落时间自动切换日间模式。此外,用户还可以根据自己的喜好,自定义日间模式的背景颜色、文本颜色和其他外观设置。 总结起来,Android日间模式切换是一个方便的功能,允许用户根据环境或个人偏好来调整应用程序的外观。通过切换到日间模式,用户可以获得更好的可读性和可见性,并且在一定程度上延长设备的电池寿命。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值