设置Roboto字体在TextView - xml



我已经找到了一些关于这个主题的帖子,但是所有这些主题要么在TextView对象上设置setTypeFace()方法的字体,要么创建一个自定义类,将字体设置为Robotoextends TextView。据我所知,从api级别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"

看一下RobotoTextView项目。直到Android 1.5,你可以使用XML属性设置字体。它还包括其他视图,如RobotoButton, RobotoCheckbox等

我不认为您可以将外部字体定义为xml属性。您应该将字体存储在资源中,并调用:

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

你不能像这样直接从资源中设置字体,你必须按照下面的onCreate操作。这和你想做的是一样的

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

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

    正常
  • 衬线
  • 等宽字体

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

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

这个话题已经在网上疯传了

对于JellyBean(4.1)以后的版本,您可以使用这个StackOverflow主题中提供的方法。在较旧的设备中,它将优雅地退回到sans。如果你必须退回到单字间距或衬线字体,声明一个文件夹布局-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);
}

这是为了将来的人遇到和我一样的问题。在加载多行时,设置字体往往会占用大量内存。使用以下两个代码一起使其顺利工作。我从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;
}
}

最新更新