Android app to set the font size and font style does not change with changes in system settings

font size

And rewriting method BaseActivity BaseApplication or overwrite method in the base class:

/设置字体为默认大小,不随系统字体大小改而改变
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        if (newConfig.fontScale != 1)//非默认值
            getResources();
        super.onConfigurationChanged(newConfig);
    }
 
 
    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        if (res.getConfiguration().fontScale != 1) {//非默认值
            Configuration newConfig = new Configuration();
            newConfig.setToDefaults();//设置默认
            res.updateConfiguration(newConfig, res.getDisplayMetrics());
        }
        return res;
    }

Font style

Use the batch set the font frame  Calligraphy

In build.gradle file, add:

implementation 'uk.co.chrisjenx:calligraphy:2.3.0'//设置字体样式

1. First, add the following statement Appliction class OnCreate method defined in their

//设置字体样式
		CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
//				.setDefaultFontPath("fonts/FZSong.ttf")
				.setDefaultFontPath("fonts/Roboto-Regular.ttf")
				.setFontAttrId(R.attr.fontPath)
				.addCustomViewWithSetTypeface(CustomViewWithTypefaceSupport.class)
				.addCustomStyle(TextField.class, R.attr.textFieldStyle)
				.build()
		);

2. BaseActivity define a class, the class inherits all the Activity, and then add the following process (replication) of

//设置字体样式
	@Override
	protected void attachBaseContext(Context newBase) {
		super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
	}

3. The font package is fonts directory under the file in the assets folder

4. In the project build.gradle added:

version = getProperty('VERSION_NAME')

    ext {
        isReleaseVersion = has("release")
        versionCodeInt = getProperty('VERSION_CODE').toInteger()
        supportLibraryVersion = '27.1.1'
        buildToolsVersion = '27.0.3'
        compileSdkVersion = 27
        minSdkVersion = 14
        targetSdkVersion = 27
    }

* Consider font copyright relations (Founder and Microsoft elegant black must not be used to pursue the prosecution), choose the source and the source in bold Times New Roman font when using the source font really nice but unfortunately there will be a line spacing problem is too large, a lot of downloading said correction version are false, only reluctantly give up, and later found a bankrupt company fandol font is also good, although the last leader did not use (last use the "Roboto-Regular.ttf")

Finally, attach some fonts, see Personal Resources Page

Siyuan blackbody

Siyuan Times New Roman font and fandol

Guess you like

Origin blog.csdn.net/P876643136/article/details/91977725