【问题标题】:How to change the Font Size in a whole Application programmatically, Android?如何以编程方式更改整个应用程序中的字体大小,Android?
【发布时间】:2012-09-24 03:05:09
【问题描述】:

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

我的需要是,如果我单击 Spinner 内的字体大小“26”,那么它应该应用于我的整个项目。就像应用到我的屏幕、Textview 外观、Edittext - 粗体/斜体等一样。如果我再次点击 46 大小,那么它应该适用于我的整个项目。

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

【问题讨论】:

标签: android font-size android-fonts


【解决方案1】:

Android 文档并未具体说明通过用户在应用程序级别选择全局更改字体大小的最有效方法。

我认为Black Devil给出的answer有问题。

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

然而,正如Siddharth Lele 在他的评论中指出的,使用stylesthemes 是处理整个应用中文本大小变化的更好方法。

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

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>

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

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();
        }
    }

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

public class AppActivity extends BaseActivity{
}

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

感谢Ray Kuhnell

【讨论】:

    【解决方案2】:

    您可以使用基本活动配置放大/缩小应用的文本大小,使所有活动成为固有基本活动。

    缩放正常值为 1.0,2.0 会使字体大小加倍,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());
    }
    

    【讨论】:

    • 效果很好。不缩放工具栏标题大小,因为这是在主题深处的“dp”中定义的。
    • 提供的代码没有考虑用户的比例设置(在设置/辅助功能/字体大小中),但可以很容易地添加。
    • 感谢这工作。为了计算用户的比例,我使用了这个:systemScale = Settings.System.getFloat(context.getContentResolver(), Settings.System.FONT_SCALE, 1f); configuration.fontScale = scale * systemScale; //剩下的代码
    • 实现该目标的最简单方法是这样......我实现了这个,我现在是一个满意的开发人员!
    【解决方案3】:

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

    就像你在 com.example 包中创建这个类并且类名是 BaseTextView,然后在 xml 文件中而不是 &lt;TextView .../&gt; 你会写 &lt;com.example.BaseTextView ... /&gt;

    希望这会有所帮助。

    【讨论】:

    • @Meena Rengarajan 你能解释一下吗,因为你已经接受了它作为正确的答案。我做了同样的事情,但没有工作。一些提示请
    【解决方案4】:

    创建一个函数并将微调器大小值传递为

    void setSize(int size){
    ...
    setTextSize()
    // on All of the layout texts and views on screen
    
    }
    

    对屏幕上的所有视图和布局文本调用 setTextSize()。

    查看文档Here

    【讨论】:

      【解决方案5】:

      要缩放所有组件(意味着整个应用程序)的字体大小,有一种很好的方法可以通过多种设备实现和测试。此解决方案可应用于以下情况;静态声明 dp 大小单位(默认为android sp),缩放到所需的字体大小等。

      该解决方案类似于 Usama Saeed US 提供的answer,但将涵盖所有错误情况。

      声明将缩放字体大小的静态 util 方法。

          //LocaleConfigurationUtil.class
          public static Context adjustFontSize(Context context){
              Configuration configuration = context.getResources().getConfiguration();
              // This will apply to all text like -> Your given text size * fontScale
              configuration.fontScale = 1.0f;
      
              return context.createConfigurationContext(configuration);
          }
      

      在所有活动中,覆盖 attachBaseContext 并在 onCreate 中调用 util 方法。

         @Override
          protected void attachBaseContext(Context newBase) {
              super.attachBaseContext(LocaleConfigurationUtil.adjustFontSize(newBase));
          }
      
         @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              LocaleConfigurationUtil.adjustFontSize(this);
          }
      

      如果您使用的是片段,则覆盖 onAttach 方法

      @Override
      public void onAttach(Context context) {
      super.onAttach(LocaleConfigurationUtil.adjustFontSize(context));
      }
      

      【讨论】:

        【解决方案6】:

        我不确定它是否有帮助。但是有一种叫做“SSP”的东西——文本的可缩放大小单元。 将此添加到您的构建 gradle 中

        implementation 'com.intuit.ssp:ssp-android:1.0.6'
        

        这个要使用

        android:textSize="@dimen/_22ssp"
        

        https://github.com/intuit/ssp

        【讨论】:

          猜你喜欢
          • 2022-12-03
          • 2013-01-09
          • 1970-01-01
          • 1970-01-01
          • 2017-03-06
          • 1970-01-01
          • 2021-07-03
          • 2023-04-08
          • 2015-08-10
          相关资源
          最近更新 更多