【问题标题】:How to prevent TextView font changing, when we change the Device font style in Samsung devices当我们在三星设备中更改设备字体样式时,如何防止 TextView 字体更改
【发布时间】:2013-10-16 14:22:35
【问题描述】:

我想说我在设置 textview 字体时遇到了一个奇怪的问题。 如果我从设置-> 显示更改设备字体样式,则 textview 字体样式也会更改。我怎样才能防止这种情况发生?

这是我的 xml 布局中的文本视图。

<TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/title_bar_bg"
        android:fontFamily="sans-serif"
        android:typeface="sans"
        android:gravity="center"
        android:text="@string/people_finder"
        android:textColor="@color/WHITE"
        android:textSize="18sp"
        android:textStyle="bold"/>

android:fontFamily="sans-serif" android:typeface="sans"

此外,它在三星 Galaxy Grand、三星 Note 2 等设备上也能正常工作。 但不适用于三星 Note 8、三星 Note 10.1 等。

还有其他方法吗?

谢谢

【问题讨论】:

    标签: android fonts textview typeface


    【解决方案1】:

    fontFamilytypeface 属性与 android 原生字体相关。如果您希望 TextViews 字体始终相同,而不管设备字体设置如何,您需要以编程方式设置自定义 Typeface

    Typeface tf = Typeface.createFromAsset(context.getAssets(), "fontName.ttf"));
    TextView textView = (TextView) findViewById(R.id.textView1);
    textView.setTypeface(tf);
    

    附带说明,Calligraphy 项目可以直接从布局 xml 中实际设置自定义字体。

    【讨论】:

    • 感谢您的回复。它可以工作,但你能告诉我为什么我们不能强制设置原生字体吗?就好像我们在等宽字体上使用衬线一样。那么它也可以工作,比如 textview.setTypeface(Typeface.MONOSPACE, Typeface.BOLD);
    • 很高兴它成功了 :) 我想这只是一个设计决定,但是是的,iOS 中也发生了类似的事情;我也想知道为什么没有直接的方法可以从 xml 布局中设置自定义字体。
    • 在 Android 11 中我们正在这样做,而三星仍在覆盖我们的字体。是否有一些新方法在 Android 11 中仍然有效?
    • @AlexArgo 你找到解决方案了吗?
    【解决方案2】:
    public class CustomTextView extends TextView {
    
        Context context;
    
        public CustomTextView (Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            this.context = context;
        }
    
        public CustomTextView (Context context, AttributeSet attrs) {
            super(context, attrs);
            this.context = context;
        }
    
        public CustomTextView (Context context) {
            super(context);
            this.context = context;
        }
    
        public void setTypeface(Typeface tf, int style) {
            if (style == Typeface.NORMAL) {
                super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/HelveticaNeue.ttf")/*
                                                                                                                 * ,
                                                                                                                 * -
                                                                                                                 * 1
                                                                                                                 */);
            } else if (style == Typeface.ITALIC) {
                super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/HelveticaNeueItalic.ttf")/*
                                                                                                                     * ,
                                                                                                                     * -
                                                                                                                     * 1
                                                                                                                     */);
            } else if (style == Typeface.BOLD) {
                super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/HelveticaNeueBold.ttf")/*
                                                                                                                     * ,
                                                                                                                     * -
                                                                                                                     * 1
                                                                                                                     */);
            }
        }
    
    }
    

    并像这样将其添加到您的 xml 中

        <xxx.xxxx.utils.CustomTextView 
            android:id="@+id/login_username_tv"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="0.3"
            android:text="Email"
            android:padding="5dp"
            android:textColor="#333333" 
            android:textSize="12dp"/>
    

    【讨论】:

    • 嗨,当我们从 .ttf 加载 svg 文件时,这种方法不起作用,即我们在 icomoon 工具的帮助下创建了 icon.ttf 文件并执行 textview.setText('letter associated with icon') 来显示图标。当设置->显示->自定义文本样式时,它仅在文本视图中加载字母而不是加载图标。当文本样式设置为默认字体时,它按预期工作。但是当文本样式设置为自定义时,即使我们执行 textview.setTypeface(iconfont) 它也不起作用。
    【解决方案3】:

    这在 kotlin 中对我有用:

    textView.typeface = ResourcesCompat.getFont(this, R.font.opensans_light)
    

    【讨论】:

      【解决方案4】:

      试试这个,它对我有用...

      @Override
      public void onConfigurationChanged(Configuration newConfig) {
          if (newConfig.fontScale != 1)
              getResources();
          super.onConfigurationChanged(newConfig);
      }
      
      @Override
      public Resources getResources() {
          Resources res = super.getResources();
          Configuration newConfig = new Configuration();
          newConfig.setToDefaults();      //Setting the default (1.0f)
          res.updateConfiguration(newConfig, res.getDisplayMetrics());
          
          return res;
      }
      

      注意:将 android:configChanges="fontScale" 添加到 Manifest &lt;activity&gt;

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-22
        • 2023-03-18
        • 1970-01-01
        • 1970-01-01
        • 2013-05-01
        • 2011-09-15
        • 2018-08-12
        相关资源
        最近更新 更多