【问题标题】:Roboto font in my android app我的 Android 应用程序中的 Roboto 字体
【发布时间】:2012-02-14 06:31:54
【问题描述】:

您好,我正在编写一个安卓应用程序。而且无论手机的版本如何,我都想要其中的机器人字体。有办法吗?

谢谢, 拉希姆。

【问题讨论】:

    标签: android fonts


    【解决方案1】:

    是的,为什么不呢,你可以得到 Roboto 字体:

    Android ICS typography

    假设您想更改文本视图的字体:

       Typeface tf = Typeface.createFromAsset(getAssets(),
                "fonts/Roboto-Black.ttf");
        TextView tv = (TextView) findViewById(R.id.FontTextView);
        tv.setTypeface(tf);
    

    【讨论】:

    • 有没有办法在 XML 本身中设置这个字体属性?或者更简单地说,在 AndroidManifest.xml 本身中,在整个应用程序中使用?
    • 我也没有找到任何方法,所以我最终得到了这个stackoverflow.com/questions/9797872/…
    【解决方案2】:

    试试这个链接http://www.barebonescoder.com/2010/05/android-development-using-custom-fonts/

    将您要定位的控件的字体属性设置为衬线...对于我推荐使用 TTF 的字体文件,它过去对我有用

    也可以试试这些链接

    http://techdroid.kbeanie.com/2011/04/using-custom-fonts-on-android.html

    Android - Using Custom Font

    【讨论】:

      【解决方案3】:

      在 XML 中设置字体稍微费力一些,但其优势在于能够在 Eclipse ADT 的 XML 布局编辑器的图形布局选项卡中预览字体。同样,首先在应用程序的资产文件夹中包含您的自定义字体 .ttf 文件。

      创建自定义文本视图类:

      public class TypefacedTextView extends TextView
      {
       public TypefacedTextView(Context context, AttributeSet attrs)
       {
        super(context, attrs);
      
         // Typeface.createFromAsset doesn't work in the layout editor. Skipping ...
         if (isInEditMode())
         {
          return;
         }
      
         TypedArray styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.TypefacedTextView);
         String fontName = styledAttrs.getString(R.styleable.TypefacedTextView_typeface);
         styledAttrs.recycle();
      
         if (fontName != null)
          {
           Typeface typeface = Typeface.createFromAsset(context.getAssets(), fontName);
           setTypeface(typeface);
          }
         }
        }
      

      现在要在您的 XML 布局中包含此自定义 TypefacedTextView,只需在 Android XML 命名空间属性下方添加您的 XML 命名空间属性:

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:your_namespace="http://schemas.android.com/apk/res/com.example.app"
      ... />
      

      并像使用 XML 中的普通 TextView 一样使用您的 TypefacedTextView,但使用您自己的自定义标签,记住设置您的字体:

      <com.example.app.TypefacedTextView
        android:id="@+id/list_item_entry_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:minHeight="48dp"
        android:textColor="#FF787878"
        your_namespace:typeface="Roboto-Regular.ttf" />
      

      查看我的博文了解更多信息: http://polwarthlimited.com/2013/05/android-typefaces-including-a-custom-font/

      【讨论】:

      • 答案中的链接上的404
      猜你喜欢
      • 1970-01-01
      • 2013-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-04
      • 1970-01-01
      • 2016-12-01
      相关资源
      最近更新 更多