如何在TextView中使用Roboto字体?

3

如何在我的文本视图中使用Roboto字体类型?我想要从XML中设置,并且我的应用程序支持4.1及以上版本。以下是我尝试过的内容:

 <style name="BubbleNumber">
    <item name="android:textStyle">normal</item>    
    <item name="android:fontFamily">sans-serif</item> 
   <item name="android:textSize">14sp</item>
   <item name="android:textColor">@color/bubble_text_color</item>
 </style>
4个回答

6

Roboto是Android 4.0开始的默认字体类型, 请参见http://developer.android.com/design/style/typography.html

否则,您需要通过编程来设置字体。

因此,我建议您编写一个类:

public class StyledTextView extends TextView {

    public StyledTextView(Context context) {
        super(context);
        style(context);
    }

    public StyledTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        style(context);
    }

    public StyledTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        style(context);
    }

    private void style(Context context) {
        Typeface tf = Typeface.createFromAsset(context.getAssets(),
                "fonts/roboto.ttf");
        setTypeface(tf);
    }

}

然后,您可以将其直接用在普通的 XML 布局中,以替换普通的 TextView。

<LinearLayout
   android:width="match_parent"
   android:height="match_parent" >

    <com.your.packakge.StyledTextView
         android:width="match_parent"
         android:height="match_parent" />

</LinearLayout>

2
为了使用Roboto字体,您需要将Roboto的.ttf文件添加到您的asset文件夹中,并将typeFace属性设置为您的文本视图。例如:
TextView title=(TextView)findViewById(R.id.tv);
Typeface font = Typeface.createFromAsset(
    activity.getAssets(), 
    "roboto.ttf");
title .setTypeface(font);

您无法在xml中设置它。


这不是开发者网站所说的。 - Abhinav Manchanda

1
我这样做是因为我的应用程序中所有字体都需要是Roboto。如果您需要控制EditText或Button,则添加一个视图类并将其扩展到自定义文本视图或可能的EditText,然后使用它。
 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 (style == Typeface.NORMAL) {
                super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/RobotoNormal.ttf")/*
                                                                                                                 * ,
                                                                                                                 * -
                                                                                                                 * 1
                                                                                                                 */);
            } else if (style == Typeface.ITALIC) {
                super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/RobotoItalic.ttf")/*
                                                                                                                     * ,
                                                                                                                     * -
                                                                                                                     * 1
                                                                                                                     */);
            } else if (style == Typeface.BOLD) {
                super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/RobotoBold.ttf")/*
                                                                                                                     * ,
                                                                                                                     * -
                                                                                                                     * 1
                                                                                                                     */);
            }
        }

    }

现在在您的 XML 布局中这样调用它:
                    <com.yourpakage.RobotoTextView
                        android:id="@+id/settings_cover_tv" 
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_centerVertical="true"
                        android:textColor="#000000"
                        android:textSize="16sp"
                        android:text="Cover Photo"
                        android:layout_marginLeft="10dp"
                        />

0

谢谢...那很有用! - MMG

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接