安卓字体设置

只修改本app的字体大小

不修改应用的字体大小 

在应用的基类里面修改。初始化然后重启

/**

 * 设置字体大小
 */
private void initFontScale() {
    Configuration configuration = getResources().getConfiguration();
    LogUtils.d("取出的字体大小是" + getSharedPreferences("text_size", MODE_PRIVATE).getFloat("text_size", 1.0f));
    configuration.fontScale = getSharedPreferences("text_size", MODE_PRIVATE).getFloat("text_size", 1.0f);
    //0.85 小, 1 标准大小, 1.15 大,1.3 超大 ,1.45 特大
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    metrics.scaledDensity = configuration.fontScale * metrics.density;
    getBaseContext().getResources().updateConfiguration(configuration, metrics);
}


。然后设置那里。

int currentSelect = 0;
if (fontScale == 1.0f) {
    currentSelect = 1;
} else if (fontScale == 0.8f) {
    currentSelect = 0;
} else {
    currentSelect = 2;
}
new AlertDialog.Builder(getContext())
        .setTitle(R.string.textsize_setting)
        .setSingleChoiceItems(textSizes, currentSelect, null)
        .setNegativeButton(android.R.string.cancel, null)
        .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                which = ((AlertDialog) dialog).getListView().getCheckedItemPosition();
                SharedPreferences sp = getContext().getSharedPreferences("text_size", Context.MODE_PRIVATE);
                float text_size = 1.0f;
                switch (which) {
                    case 0:
                        text_size = 0.8f;
                        break;
                    case 1:
                        text_size = 1.0f;
                        break;
                    case 2:
                        text_size = 1.2f;
                        break;

                }
                sp.edit().putFloat("text_size", text_size).commit();
                Toast.makeText(getContext(), R.string.setting_language_restart_app_hint, Toast.LENGTH_SHORT).show();
                binding.getRoot().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        AppUtils.restartApp(getActivity());
                    }
                }, 1000);
            }
        }).show();



你可能感兴趣的:(安卓字体设置)