克拉玛依网站建设公司,国内做视频课程的网站有哪些,广州 网站开发公司,沈阳网站关键词优化公司一.概述
正在开发的应用做了一版新UI#xff0c;原打算将新版UI按项目名做成资源包#xff0c;再在build.gradle里productFlavors{ }多渠道打包实现
但被告知新旧两个项目共用一个分支#xff0c;那就做成两个主题(Theme1/Theme2)来适配了
如果只是变更UI#xff0c;做成…一.概述
正在开发的应用做了一版新UI原打算将新版UI按项目名做成资源包再在build.gradle里productFlavors{ }多渠道打包实现
但被告知新旧两个项目共用一个分支那就做成两个主题(Theme1/Theme2)来适配了
如果只是变更UI做成多主题来适配不同项目也是比较合适的方式
一.Android App实现多主题的主要有如下几种 从服务器下载或提前放置不同主题资源包到设备指定目录在代码里引用主题资源定义不同的style、使用Android的setTheme()机制设置不同主题将主题资源做成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 version1.0 encodingutf-8?
resources attr nameColorAttrName formatcolor / attr nameintegerAttrName formatinteger / attr namebooleanAttrName formatboolean / attr namedimensionAttrName formatdimension / item namefloatAttrName formatdimension / attr namestringAttrName formatstring / attr namereferenceAttrName formatreference /
/resources reference主要对应drawable、mipmap资源其他的看字面意思
2.2 定义主题(Theme1/Theme2)
/values/style.xml中定义Theme1、Theme2 style nameThemeBlack parentandroid:style/Theme.Black item namecolorAttrName#FF00FF00/item item nameintegerAttrName33/item item namebooleanAttrNametrue/item item namedimensionAttrName76dp/itemitem namefloatAttrName0.35/item item namestringAttrNamestring/hello_world/item item namereferenceAttrNamedrawable/hand/item
/stylestyle nameThemeLight parentandroid:style/Theme.Light item namecolorAttrName#FFFFFF00/item item nameintegerValue55/item item namebooleanAttrNamefalse/item item namedimensionValue76px/item item namefloatAttrName0.15/item item namestringAttrNamestring/action_settings/item item namereferenceAttrNamedrawable/ic_launcher/item
/style 各属性的具体值可以在Theme定义时配置但是最好定义在各自的xml配置文件中(color.xml、string.xml、dimens.xml.....)然后在Theme定义时类型/...格式引用
Theme中如果string类型不是引用而是字符串在布局文件中使用正常但代码里获取会有问题
2.3 布局文件中引用属性
使用过程也很简单原本布局文件中引用UI属性的方式如下 android:colorcolor/colorAttrName
android:durationinteger/integerValue
android:adjustViewBoundsbool/booleanAttrName
android:layout_heightdimen/dimensionValue
android:textstring/stringAttrName
android:backgrounddrawable/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 {Overrideprotected 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 {Overrideprotected 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 {Overrideprotected 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 ContextContext remoteContext createPackageContext(remotePackage,CONTEXT_IGNORE_SECURITY);//主题资源Apk ResourcesResources 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主题包中提供。