41 lines
1.3 KiB
Java
41 lines
1.3 KiB
Java
package com.xscm.moduleutil.utils;
|
||
|
||
import android.app.Activity;
|
||
import android.content.Context;
|
||
import android.content.res.Configuration;
|
||
import android.content.res.Resources;
|
||
|
||
public class DisplayUtil {
|
||
/**
|
||
* 保持字体大小不随系统设置变化(用在界面加载之前)
|
||
* 要重写Activity的attachBaseContext()
|
||
*/
|
||
public static Context attachBaseContext(Context context, float fontScale) {
|
||
Configuration config = context.getResources().getConfiguration();
|
||
//正确写法
|
||
config.fontScale = fontScale;
|
||
return context.createConfigurationContext(config);
|
||
}
|
||
|
||
/**
|
||
* 保持字体大小不随系统设置变化(用在界面加载之前)
|
||
* 要重写Activity的getResources()
|
||
*/
|
||
public static Resources getResources(Context context, Resources resources, float fontScale) {
|
||
Configuration config = resources.getConfiguration();
|
||
if(config.fontScale != fontScale) {
|
||
config.fontScale = fontScale;
|
||
return context.createConfigurationContext(config).getResources();
|
||
} else {
|
||
return resources;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 保存字体大小,后通知界面重建,它会触发attachBaseContext,来改变字号
|
||
*/
|
||
public static void recreate(Activity activity) {
|
||
activity.recreate();
|
||
}
|
||
}
|