我的android应用程序中的Roboto字体

我的android应用程序中的Roboto字体,android,fonts,Android,Fonts,嗨,我正在写一个android应用程序。我想在它的roboto字体,无论手机的版本。有办法吗 谢谢, 拉希姆。是的,为什么不呢,你可以得到Roboto字体: 假设您要更改文本视图的字体: Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Black.ttf"); TextView tv = (TextView) findViewById(R.id.FontTextView);

嗨,我正在写一个android应用程序。我想在它的roboto字体,无论手机的版本。有办法吗

谢谢,
拉希姆。

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

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

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

将目标控件的typeface属性设置为serif。。。对于我推荐使用TTF的字体文件,它在过去对我很有效

也可以尝试这些链接


在XML中设置字体需要花费更多的精力,但其优点是能够在XML布局编辑器的Eclipse ADT图形布局选项卡中预览字体。同样,首先在应用程序的资产文件夹中包含自定义font.ttf文件

创建自定义textview类:

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中的普通文本视图一样使用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" />

有关更多信息,请参阅我的博客帖子:

有没有办法在XML本身中设置此typeface属性?或者更简单地说,在AndroidManifest.xml中,在整个应用程序中使用?我也没有找到任何方法来实现这一点,所以我在答案中的链接上使用了404