从设置中可以看到字体设置有四个等级"小,默认,大,最大",一般都是使用默认的。有的产品使用默认字体会显大或者显小,我们需要根据实际需求来修改。
1.字体大小设置相关内容
- 源码:/packages/apps/Settings/res/xml/display_settings.xml
<SwitchPreference
android:key="peak_refresh_rate"
android:title="@string/peak_refresh_rate_title"
android:summary="@string/peak_refresh_rate_summary"
settings:controller="com.android.settings.display.PeakRefreshRatePreferenceController" />
<!-- 字体大小设置 -->
<Preference
android:key="font_size"
android:title="@string/title_font_size"
android:fragment="com.android.settings.display.ToggleFontSizePreferenceFragment"
settings:controller="com.android.settings.display.FontSizePreferenceController" />
<!-- 显示大小设置-->
<com.android.settings.display.ScreenZoomPreference
android:key="display_settings_screen_zoom"
android:title="@string/screen_zoom_title"
android:fragment="com.android.settings.display.ScreenZoomSettings"/>
可以看到字体设置显示和控制相关的fragment和controller。
- 源码:/packages/apps/Settings/src/com/android/settings/display/FontSizePreferenceController.java
public class FontSizePreferenceController extends BasePreferenceController {
public FontSizePreferenceController(Context context, String key) {
super(context, key);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public CharSequence getSummary() {
final float currentScale = Settings.System.getFloat(mContext.getContentResolver(),
Settings.System.FONT_SCALE, 1.0f);
final Resources res = mContext.getResources();
final String[] entries = res.getStringArray(R.array.entries_font_size);
final String[] strEntryValues = res.getStringArray(R.array.entryvalues_font_size);
final int index = ToggleFontSizePreferenceFragment.fontSizeValueToIndex(currentScale,
strEntryValues);
return entries[index];
}
}
- 源码:/packages/apps/Settings/src/com/android/settings/display/ToggleFontSizePreferenceFragment.java
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Resources res = getContext().getResources();
final ContentResolver resolver = getContext().getContentResolver();
// Mark the appropriate item in the preferences list.
mEntries = res.getStringArray(R.array.entries_font_size);
final String[] strEntryValues = res.getStringArray(R.array.entryvalues_font_size);
final float currentScale =
Settings.System.getFloat(resolver, Settings.System.FONT_SCALE, 1.0f);
mInitialIndex = fontSizeValueToIndex(currentScale, strEntryValues);
mValues = new float[strEntryValues.length];
for (int i = 0; i < strEntryValues.length; ++i) {
mValues[i] = Float.parseFloat(strEntryValues[i]);
}
getActivity().setTitle(R.string.title_font_size);
}
@Override
protected Configuration createConfig(Configuration origConfig, int index) {
// Populate the sample layouts.
final Configuration config = new Configuration(origConfig);
config.fontScale = mValues[index];
return config;
}
/**
* Persists the selected font size.
*/
@Override
protected void commit() {
if (getContext() == null) return;
final ContentResolver resolver = getContext().getContentResolver();
Settings.System.putFloat(resolver, Settings.System.FONT_SCALE, mValues[mCurrentIndex]);
}
可以看到代码,字体设置中显示和设置都是操作Settings.System.FONT_SCALE这个系统属性值,当你设置字体大小后会先改变这个属性的值,然后系统监听到这个属性值变化之后去改变整个系统字体大小。具体的更新系统字体大小的逻辑在ActivityTaskManagerService这个服务中。
- 源码:frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
private final class FontScaleSettingObserver extends ContentObserver {
private final Uri mFontScaleUri = Settings.System.getUriFor(FONT_SCALE);
private final Uri mHideErrorDialogsUri = Settings.Global.getUriFor(HIDE_ERROR_DIALOGS);
public FontScaleSettingObserver() {
super(mH);
final ContentResolver resolver = mContext.getContentResolver();
resolver.registerContentObserver(mFontScaleUri, false, this, UserHandle.USER_ALL);
resolver.registerContentObserver(mHideErrorDialogsUri, false, this,
UserHandle.USER_ALL);
}
@Override
public void onChange(boolean selfChange, Collection<Uri> uris, int flags,
@UserIdInt int userId) {
for (Uri uri : uris) {
if (mFontScaleUri.equals(uri)) {
updateFontScaleIfNeeded(userId);
} else if (mHideErrorDialogsUri.equals(uri)) {
synchronized (mGlobalLock) {
updateShouldShowDialogsLocked(getGlobalConfiguration());
}
}
}
}
}
...
private void updateFontScaleIfNeeded(@UserIdInt int userId) {
final float scaleFactor = Settings.System.getFloatForUser(mContext.getContentResolver(),
FONT_SCALE, 1.0f, userId);
synchronized (mGlobalLock) {
if (getGlobalConfiguration().fontScale == scaleFactor) {
return;
}
final Configuration configuration
= mWindowManager.computeNewConfiguration(DEFAULT_DISPLAY);
configuration.fontScale = scaleFactor;
updatePersistentConfiguration(configuration, userId);
}
}
所以我们想要设置默认字体大小,直接修改Settings.System.FONT_SCALE这个属性值。
2.修改默认字体大小
- 源码:/packages/apps/Settings/res/values/arrays.xml
<string-array name="entries_font_size">
<item msgid="6490061470416867723">Small</item>
<item msgid="3579015730662088893">Default</item>
<item msgid="1678068858001018666">Large</item>
<item msgid="490158884605093126">Largest</item>
</string-array>
<string-array name="entryvalues_font_size" translatable="false">
<item>0.85</item>
<item>1.0</item>
<item>1.15</item>
<item>1.30</item>
</string-array>
这个数组是字体大小设置的配置,比如我们想要设置系统字体默认最小,添加如下配置:
- 源码:/frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
private void loadSystemSettings(SQLiteDatabase db) {
SQLiteStatement stmt = null;
try {
stmt = db.compileStatement("INSERT OR IGNORE INTO system(name,value)"
+ " VALUES(?,?);");
...
// add for set default font
loadFractionSetting(stmt, Settings.System.FONT_SCALE,
R.fraction.def_font_size_scale, 1);
// add end
- 源码:/frameworks/base/packages/SettingsProvider/res/values/defaults.xml
<!-- Default font size 0.85/1.0/1.15/1.30 -->
<fraction name="def_font_size_scale">85%</fraction>