apk适应深浅主题变化,在配置资源时分别在values、values-night中添加了默认资源(可以对应白天模式)和黑夜资源,假设应用使用的默认主题为APPTheme,其中定义如下:manifest.xml
...其他组件注册...
而引用的样式无非就是关联需要使用的属性对应的值而已,这些属性我们也可以在values中自定义,如以下应用使用的公共属性(以文本颜色、以及背景图为例)values/attrs.xml:
浅色样式values/styles.xml:
深色样式values-night/styles.xml:
该方案从样式出发,在深浅样式中需要自行配置对应的资源,所以对于drawable、color之类的资源可以放在相同的位置,然后在名称上区分即可。最后在布局中直接由自定义属性来导入资源(引用方式为"?attr/xxx"),如:layout/main_activity.xml
基于以上样式的配置,应用中无需定义多套布局文件就可以适配深浅主题的变化,只要在变化时重新加载布局(或者根据属性名称重新引入资源配置到各个View上)即可。
自定义样式兼容其他UI同样的道理,如果需要在此基础上适配其他UI效果,那么可以再自定义其他样式,然后应用样式即可,假设以上适应深浅主题变化的配置为系列A的UI效果,现在要扩展一个系列B的UI效果(有可能有自己的深浅样式),那么可以定义如下(假设样式名称为APPThemeB):
注意:自定义属性无需变化
浅色样式values/styles.xml:
为了保留默认样式中的某些公共效果,可以在扩展样式时直接继承默认样式
深色样式values-night/styles.xml:
注意:布局中引用方式不变
到这里针对系列A以及系列B的样式已经定义完毕,但是在manifest.xml中已经默认配置的样式为APPTheme,那么在识别到系列B的设备时动态切换为APPThemeB就成为该兼容适配的关键。这里以Activity为例(也可以在Application或者Service中进行):
- 对所有需要自适应样式的Activity定义一个样式运用规则,如果需要使用自定义样式,则直接使用setTheme(int theme)方法设置,否则使用默认样式。
- 在初次加载以及系统配置发生变化(这里主要是深浅配置的变化)时引用样式。
基于以上规则,定义一个自适应样式超类,如BaseActivity.java
public abstract class BaseActivity extends FragmentActivity{
private int currentUiMode = -1; //记录当前深浅模式
//获取主题
protected int getThemeStyle(boolean modeNight) {
if (UIStyleConfig.isDevicesB()) {
return R.style.APPThemeB;
}
return 0;
}
//运用主题
private void applyTheme(int theme) {
if (theme != 0) {
setTheme(theme);
}
initUI();
}
//初次进入时配置主题
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
currentUiMode = getResources().getConfiguration().uiMode;
int uiModeNight = currentUiMode & Configuration.UI_MODE_NIGHT_MASK;
applyTheme(getThemeStyle(uiModeNight == Configuration.UI_MODE_NIGHT_YES));
}
//各个子类在该方法中设置布局文件即可(setContentView)
protected abstract void initUI();
//配置变化时配置主题
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (currentUiMode != -1 && currentUiMode != newConfig.uiMode) {
int nightMode = newConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK;
int theme = getThemeStyle(nightMode == Configuration.UI_MODE_NIGHT_YES);
applyTheme(theme);
currentUiMode = newConfig.uiMode;
onUIModeChanged(true);
}
}
//深浅样式发生变化
protected void onUIModeChanged(boolean modeNight) {
...
}
}
需要兼容样式的页面直接从BaseActivity类继承即可,如MainActivity.java
public class MainActivity extends BaseActivity {
@Override
protected void initUI() {
setContentView(R.layout.main_activity);
...
}
}
这里需要注意的是,通过onConfigurationChanged监听UI变化需要在manifest中对应的Activity的configChanges中配置上uiMode即可,如该MainActivity:
到此,app兼容不同UI样式配置完毕,后续有其他UI效果则直接新增样式,然后在BaseActivity的getThemeStyle(…)方法中新增样式关联即可。
附:java中根据自定义属性加载所属样式下的值//获取颜色值
public static int getColor(Context context, int attr) {
int[] attrsArray = {attr};
TypedArray typedArray = context.obtainStyledAttributes(attrsArray);
int color = typedArray.getColor(0, Color.WHITE);
typedArray.recycle();
return color;
}
//获取状态列表颜色对象(比如:选中、未选中、常规、按下、可用、不可用等等)
public static ColorStateList getColorStateList(Context context, int attr) {
int[] attrsArray = {attr};
TypedArray typedArray = context.obtainStyledAttributes(attrsArray);
ColorStateList colors = typedArray.getColorStateList(0);
typedArray.recycle();
return colors;
}
//获取资源id
public static int getResId(Context context, int attr) {
int[] attrsArray = {attr};
TypedArray typedArray = context.obtainStyledAttributes(attrsArray);
int id = typedArray.getResourceId(0, 0);
typedArray.recycle();
return id;
}
//获取drawable对象
public static Drawable getDrawable(Context context, int attr) {
int[] attrsArray = {attr};
TypedArray typedArray = context.obtainStyledAttributes(attrsArray);
Drawable d = typedArray.getDrawable(0);
typedArray.recycle();
return d;
}



