【问题标题】:How to use Roboto Font in TextView?如何在 TextView 中使用 Roboto 字体?
【发布时间】:2013-09-19 08:26:35
【问题描述】:

如何为我的文本视图使用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>

【问题讨论】:

    标签: android fonts styles textview


    【解决方案1】:

    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字体类型,你必须在你的asset文件夹中添加roboto的.ttf文件。并将 typeFace 属性设置为您的文本视图。例如

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

      你不能在xml中设置它。

      【讨论】:

      • 这不是开发者网站所说的。
      【解决方案3】:

      我已经在我的应用程序中这样做了,因为我需要的只是所有字体都是 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"
                              />
      

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • 谢谢...这很有用!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-11-08
        • 1970-01-01
        • 2015-07-16
        • 1970-01-01
        • 2015-11-02
        • 2012-11-12
        • 1970-01-01
        相关资源
        最近更新 更多