【问题标题】:Setting Roboto font in TextView - xml在 TextView 中设置 Roboto 字体 - xml
【发布时间】:2013-03-31 19:06:38
【问题描述】:

我找到了几篇关于这个主题的帖子,但是所有这些主题要么使用 TextView 对象上的 setTypeFace() 方法设置字体,要么创建一个自定义类将字体设置为 Roboto 和 @987654324 @TextView。据我从 API-Level 11(?) 或其他方面了解,我们能够以某种方式将 TypeFace 设置为 xml 属性。像这样:

   <TextView
      android:id="@+id/profileHeader"
      android:layout_width="100dp"
      android:layout_height="100dp"
      android:typeface="roboto"
      android:text="Hello, world">
   </TextView>

这样做的正确方法是什么?如果应用程序在低于 API 级别 11(?) 的设备上运行,是否可以进行回退,例如:

 android:typeface="roboto|monospace|serif"

【问题讨论】:

  • 我认为除了默认字体之外,没有其他方法可以在 Android TextView 中从 XML 设置字体。

标签: android textview


【解决方案1】:

看看RobotoTextView 项目。适用于 Android 1.5,您可以使用 XML 属性设置字体。它还包括其他视图,如 RobotoButton、RobotoCheckbox 等。

【讨论】:

    【解决方案2】:

    我没有看到可以将外部字体定义为 xml 属性的方法。您应该将字体存储在资产中并调用:

    tv.setTypeface( Typeface.createFromAsset( context.getAssets(), roboto.ttf ) );
    

    【讨论】:

      【解决方案3】:

      您不能直接从这样的资产设置字体,您必须在 onCreate 中执行以下操作。这将使你想做同样的事情。

      TextView tvTextView = (TextView) findViewById(R.id.textView1);
      Typeface typeface = Typeface.createFromAsset(getAssets(),"Roboto.ttf");
      tvTextView.setTypeface(typeface);
      

      希望对你有所帮助。:D

      【讨论】:

        【解决方案4】:

        android:typeface 属性只有几个有效选项(根据 Android 文档)...

        • 正常
        • 没有
        • 衬线
        • 等宽

        如果您需要在旧设备的应用中使用 Roboto 字体,则需要将 Roboto TTF 文件包含到您的项目中。

        使用这些字体最明显的方法是使用 TextView 的 setTypeface() 方法,但如果您想改为在 XML 中指定它,则必须创建自定义 TextView 并为自定义 TextView 创建自己的 styleable 属性。

        This topic is all over the Internet.

        【讨论】:

          【解决方案5】:

          对于 JellyBean (4.1) 以后的版本,您可以使用this StackOverflow topic 中提供的方法。它将优雅地回退到旧设备中的 sans。 如果您必须回退到等宽或衬线,请声明一个文件夹 layout-v16,在其中使用您选择的字体,即“sans-serif-condensed”,并在默认文件夹中使用“等宽”或“衬线”字体。 如果您想回退到非默认字体,您可以通过编程方式检查 android 版本并选择适当的操作,即:

          if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
              TextView textView = (TextView) findViewById(R.id.textView_id);
              Typeface myFont = Typeface.createFromAsset(getAssets(),"RobotoCondensed.ttf");
              textView.setTypeface(myFont);
          }
          

          【讨论】:

            【解决方案6】:

            这是为将来遇到与我相同问题的人准备的。在加载多行时,设置字体往往会占用大量内存。将以下两个代码一起使用,实际上可以使其顺利运行。我从 stackoverflow 获得了解决方案,但他们的答案没有一起列出。

            public class RobotoTextView extends TextView {
            
            Context context;
            
            public RobotoTextView(Context context, AttributeSet attrs, int defStyle) {
                super(context, attrs, defStyle);
                this.context = context;
            }
            
            public RobotoTextView(Context context, AttributeSet attrs) {
                super(context, attrs);
                this.context = context;
            }
            
            public RobotoTextView(Context context) {
                super(context);
                this.context = context;
            }
            
            public void setTypeface(Typeface tf, int style) {
                if (!isInEditMode()) {
                    if (style == Typeface.NORMAL) {
                        super.setTypeface(TypeFaceProvider.getTypeFace(getContext(), "fonts/Roboto-Light.ttf"));
                    } else if (style == Typeface.ITALIC) {
                        super.setTypeface(TypeFaceProvider.getTypeFace(getContext(), "fonts/Roboto-LightItalic.ttf"));
                    } else if (style == Typeface.BOLD) {
                        super.setTypeface(TypeFaceProvider.getTypeFace(getContext(), "fonts/Roboto-Bold.ttf"));
                    } else if (style == Typeface.BOLD_ITALIC) {
                        super.setTypeface(TypeFaceProvider.getTypeFace(getContext(), "fonts/Roboto-BoldItalic.ttf"));
                    }
            
                }
            }
            
            
            
            public class TypeFaceProvider {
            
            private static Hashtable<String, Typeface> sTypeFaces = new Hashtable<String, Typeface>(
                    4);
            
            public static Typeface getTypeFace(Context context, String fileName) {
                Typeface tempTypeface = sTypeFaces.get(fileName);
            
                if (tempTypeface == null) {
                    tempTypeface = Typeface.createFromAsset(context.getAssets(),
                            fileName);
                    sTypeFaces.put(fileName, tempTypeface);
                }
            
                return tempTypeface;
            }
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-10-04
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多