2025年Android:主题切换

Android:主题切换一 概述 正在开发的应用做了一版新 UI 原打算将新版 UI 按项目名做成资源包 再在 build gradle 里 productFlavo 多渠道打包实现 但被告知新旧两个项目共用一个分支 那就做成两个主题 Theme1 Theme2 来适配了 如果只是变更 UI 做成多主题来适配不同项目也是比较合适的方式 一

大家好,我是讯享网,很高兴认识大家。

一.概述

正在开发的应用做了一版新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主题包中提供。

小讯
上一篇 2025-03-02 23:38
下一篇 2025-02-06 12:22

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/34550.html