【问题标题】:How to make font sizes in app to not get effected by changing Settings Font size [duplicate]如何使应用程序中的字体大小不受更改设置字体大小的影响[重复]
【发布时间】:2014-02-04 06:58:07
【问题描述】:

当字体大小在设置->显示->字体大小上为“正常”时,我的应用程序按照设计运行良好。

当我将字体大小更改为小/大时,整个内容都会被混搭。我的应用程序内容密集,这对应用程序产生了严重影响。

可以说,操作栏标题类似于“隐私和设置”。当系统设置中的字体大小设置为“大”时,标题会超出边界/省略号。 listview 的内容看起来很可怜​​。

有没有办法确保 Settings->Fonts 中的更改不会影响我们的应用程序?

Flipboard 应用程序可以做到这一点。

【问题讨论】:

  • 您确实意识到,提高默认字体大小的确切用户是无法阅读较小字体并因此故意更改字体的人?
  • 是的,我同意这些设置是为了用户的利益。但这就是要求。

标签: android android-layout android-listview


【解决方案1】:

我终于找到了解决方案。 Google 强烈建议对字体使用“sp”。但是,如果您不希望根据用户字体设置缩放文本,请使用“dp”。如果您转到设置->字体,字体大小在给定设备中不会改变。您的 UI 将保持原样。

感谢 Aswin 的投入。感谢 Joseph Earl 在Android sp vs dp texts - what would adjust the 'scale' and what is the philosophy of support

上回答问题

【讨论】:

【解决方案2】:

最好遵循 Google 的建议,即使用“sp”而不是“dp”。这是一个帮助我的更新解决方案 - Solution

 //in base activity add this code.
public  void adjustFontScale( Configuration configuration) {

    configuration.fontScale = (float) 1.0;
    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());
}  

【讨论】:

    【解决方案3】:

    在 textSize 中使用 dp 或 dip 代替 sp

    【讨论】:

      【解决方案4】:

      你可以这样做,但我不推荐它。

      float scale = getResources().getConfiguration().fontScale;
      // This scale tells you what the font is scaled to.
      // So if you want size 16, then set the size as 16/scale
      float sizeNeeded = 16.0;
      textView.setTextSize(sizeNeeded/scale);
      

      希望这会有所帮助。

      【讨论】:

      • 我读起来像,我应该自定义 Textview 并覆盖 setTextSize 并在那里添加这个缩放计算。将此用于应用程序中使用的所有文本视图。
      猜你喜欢
      • 2019-03-09
      • 2018-09-26
      • 2022-09-28
      • 2017-09-19
      • 2023-01-22
      • 2011-09-07
      • 2013-03-17
      • 1970-01-01
      • 2020-12-21
      相关资源
      最近更新 更多