android - 文本大小(dp)

标签 android xml android-layout textview

所以我现在完全迷路了,当用户通过Accessibility -> Font size 将字体大小时设置为Huge 时,文本大小会破坏我的 UI,所以为了快速修复 我决定将所有文本大小更改为 dp 而不是 sp 文本将始终具有相同的大小,但在某些地方 textviews 即使 textsize 设置为 dp 调整大小知道为什么吗??

这是文本大小设置为 dp 的按钮,即使在通过辅助功能更改后也能保持字体大小

    <Button
        android:id="@+id/video_button"
        android:textSize="16dp"
        android:layout_marginTop="10dp"
        android:layout_height="30dp"
        android:layout_width="180dp"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/webshop_button"
        android:text="@string/button_3dvideo_text"/>

这里是 textView,即使在将其设置为 dp 后也会忽略 textSize:

 <TextView
        android:id="@+id/about"
        android:gravity="center"
        android:textSize="16dp"
        android:layout_above="@+id/footer"
        android:layout_marginBottom="20dp"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/color_white"
        android:text="@string/about"/>

最佳答案

我有一个解决方案,可以使您的应用程序中的字体大小不可更改:

 public void adjustFontScale(Configuration configuration) {
    configuration.fontScale = (float) 1;
    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);
}

在每个 Activity 的 onCreate 中调用此方法,或者您可以创建一个 Base Activity:

 adjustFontScale(getResources().getConfiguration());

您可以根据需要更改 fontScale。 希望这会有所帮助。

关于android - 文本大小(dp),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47261909/

相关文章:

java - Android onPreExecute方法报奇怪错误

PHP XMLWriter,如何插入换行符?

c# - 当 minOccurs 属性 > 1 时使用默认属性有什么好处?

android - 自定义 ListView 来自数据库我想点击 ListView 意味着它打印特定的项目名称

Android 小部件大小不正确

javascript - 在 Android 和 iPhone 的 JS 链接标签上触发点击事件

java - 在 Activity 中重用 fragment 类

java - 内存不足错误?

objective-c - 从 Objective-C 导航 XML

Android fragment 屏幕重叠