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

4 回答 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>
于 2013-09-19T08:48:59.793 回答
2

要实现roboto字体类型,您必须在资产文件夹中添加roboto的.ttf文件。并将 typeFace 属性设置为您的文本视图。例如

TextView title=(TextView)findViewById(R.id.tv);
Typeface font = Typeface.createFromAsset(
    activity.getAssets(), 
    "roboto.ttf");
title .setTypeface(font);

你不能在xml中设置它。

于 2013-09-19T08:44:08.717 回答
1

我已经在我的应用程序中这样做了,因为我需要的只是所有字体都是 Roboto。如果您需要控制 edittext 或在按钮中添加一个视图类并在自定义文本视图中扩展它,或者可能是 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"
                        />
于 2013-09-19T10:02:52.523 回答
0

还有一个库Android-RobotoTextView可以做到这一点。

于 2015-01-13T10:58:39.367 回答