如何以编程方式更改整个应用程序中的字体大小,Android?

Ach*_*ver 18 android font-size android-fonts

我创建了Spinner,其字体大小从"8"到"46".我可以单击字体大小并在它向我显示的微调器中.

我需要的是,如果我在Spinner中单击字体大小"26",那么它应该应用于我的整个项目.就像应用于我的屏幕,Textview外观,Edittext - Bold/Italic等.再次,如果我点击46大小,那么它应该适用于我的整个项目.

我怎么能通过编程方式做到这一点?

Rit*_*une 23

Android 文档并不特定于通过用户在应用程序级别选择全局更改字体大小的最有效方法.

我认为Black Devil给出的答案存在问题.

问题是,许多的Android小部件子类TextView,如Button,RadioButtonCheckBox.其中一些是间接子类TextView,这使得TextView在这些类中实现定制版本非常困难.

然而正如Siddharth Lele在他的评论中指出的那样,使用styles或者themes是更好的方法来处理整个应用程序中文本大小的变化.

我们为布局设置样式以控制视图的外观.主题基本上只是这些风格的集合.但是,我们可以使用主题仅用于文本大小设置; 没有为每个属性定义值.使用样式主题为我们提供了一个巨大的优势:我们可以以编程方式为整个视图设置主题.

theme.xml

<resources>
    <style name="FontSizeSmall">
        <item name="android:textSize">12sp</item>
    </style>
    <style name="FontSizeMedium">
        <item name="android:textSize">16sp</item>
    </style>
    <style name="FontSizeLarge">
        <item name="android:textSize">20sp</item>
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

创建一个类来处理加载我们的首选项:

public class BaseActivity extends Activity {
    @Override
    public void onStart() {
        super.onStart();

        // Enclose everything in a try block so we can just
        // use the default view if anything goes wrong.
        try {
            // Get the font size value from SharedPreferences.
            SharedPreferences settings =
                getSharedPreferences("com.example.YourAppPackage", Context.MODE_PRIVATE);

            // Get the font size option.  We use "FONT_SIZE" as the key.
            // Make sure to use this key when you set the value in SharedPreferences.
            // We specify "Medium" as the default value, if it does not exist.
            String fontSizePref = settings.getString("FONT_SIZE", "Medium");

            // Select the proper theme ID.
            // These will correspond to your theme names as defined in themes.xml.
            int themeID = R.style.FontSizeMedium;
            if (fontSizePref == "Small") {
                themeID = R.style.FontSizeSmall;
            }
            else if (fontSizePref == "Large") {
                themeID = R.style.FontSizeLarge;
            }

            // Set the theme for the activity.
            setTheme(themeID);
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
Run Code Online (Sandbox Code Playgroud)

最后,通过扩展BaseActivity来创建活动,如下所示:

public class AppActivity extends BaseActivity{
}
Run Code Online (Sandbox Code Playgroud)

由于大多数应用程序的活动量比继承TextView的TextView或小部件少得多.这将是指数级的,因为复杂性增加,因此该解决方案需要较少的代码更改.

感谢Ray Kuhnell


Usa*_* US 10

您可以使用基本活动配置向上/向下缩放到应用程序的文本大小,使所有活动固有于基本活动。

缩放正常值为1.0,2.0将使字体大小加倍,而.50将使字体大小减半。

public  void adjustFontScale( Configuration configuration,float scale) {

    configuration.fontScale = scale;
    DisplayMetrics metrics = getResources().getDisplayMetrics();
    WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    wm.getDefaultDisplay().getMetrics(metrics);
    metrics.scaledDensity = configuration.fontScale * metrics.density;
    getBaseContext().getResources().updateConfiguration(configuration, metrics);

}

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    adjustFontScale( getResources().getConfiguration());
}
Run Code Online (Sandbox Code Playgroud)

  • 提供的代码没有考虑用户的比例设置(在“设置/辅助功能/字体大小”中),但可以很容易地添加。 (2认同)
  • 谢谢这有效。为了考虑用户的规模,我使用了这个: systemScale = Settings.System.getFloat(context.getContentResolver(), Settings.System.FONT_SCALE, 1f); 配置.fontScale = 比例 * systemScale; //其余代码 (2认同)

And*_*oid 5

可能的解决方案将是您创建一个基类,其扩展的TextView,并使用该文本视图类为编辑文本。希望您在第一个屏幕中要求尺寸。在任何情况下,您都可以在基类中设置文本大小。这将解决您的问题。

就像你在包 com.example 中创建这个类,类名是 BaseTextView,然后在 xml 文件中而不是<TextView .../> 你会写<com.example.BaseTextView ... />

希望这可以帮助。