【问题标题】:Application text size not scaling with system font size应用程序文本大小不随系统字体大小缩放
【发布时间】:2017-10-01 12:26:12
【问题描述】:

我必须在现有的 Android 应用程序上工作,其中所有文本都在布局 XML 文件中的 SP 单元中提及。

但是,应用程序的文本大小不会随系统字体一起调整。即,如果我从Settings -> Accessibility -> Font Size 更改系统字体大小,则应用程序的字体大小根本不会改变。

PFB 摘自我的布局文件,其中在 SP 中提到了 TextView's textSize。

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="12dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:gravity="center"
        android:text="@string/find_an_noffice"
        android:textSize="12sp"/>
</LinearLayout>

我认为这个问题与布局结构无关,因此我在示例应用程序中使用了相同的布局文件。此示例应用程序按预期工作。即应用的字体大小随系统字体调整。

我的原始应用程序没有像示例应用程序那样运行的可能性有哪些?

任何帮助将不胜感激。

【问题讨论】:

  • 所以,要明确一点:同样的布局在 App A 中有效,但在 App B 中无效?如果是这样,这两个应用程序有什么不同?例如,它们的主题是否不同?
  • @CommonsWare:感谢您的评论。是的,相同的布局在 AppA 中而不是在 AppB 中工作。他们都使用相同的主题Theme.AppCompat.Light.NoActionBar。这也是让我感到困惑的地方。字体不缩放还有其他可能性吗?
  • 什么都没想到。
  • @CommonsWare 我得到了解决方案。请看一看。

标签: android fonts sp scale-independent


【解决方案1】:

我找到了问题的根本原因。我的应用正在创建一个新的 Configuration,这导致了问题。

导致此问题的代码:

Locale locale = new Locale(isSpanish() ? "es" : "en");
Locale.setDefault(locale);

Configuration config = new Configuration();
config.locale = locale;
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());

如果我将Configuration config = new Configuration(); 行更改为Configuration config = context.getResources().getConfiguration();,应用程序字体将根据系统字体大小的变化进行缩放。

希望这可以帮助某人。

【讨论】:

    猜你喜欢
    • 2021-09-26
    • 2020-07-19
    • 2023-03-22
    • 2016-10-02
    • 2023-03-22
    • 2022-01-26
    • 1970-01-01
    • 2020-01-28
    • 2013-12-26
    相关资源
    最近更新 更多