Roboto font in my android app
嗨,我正在编写一个Android应用程序。而且我希望无论手机的版本如何,都可以使用roboto字体。有办法吗?
谢谢,
拉希姆
是的,您可以获取Roboto字体:
Android ICS typography
假设您要更改文本视图的字体:
1 2 3 4 | Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Black.ttf"); TextView tv = (TextView) findViewById(R.id.FontTextView); tv.setTypeface(tf); |
设置XML字体的工作量稍为增加,但具有能够在XML布局编辑器的Eclipse ADT的图形布局选项卡中预览字体的优点。同样,首先将自定义字体.ttf文件包含在应用程序的资产文件夹中。
创建自定义textview类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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名称空间属性:
1 2 3 | <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,但要使用您自己的自定义标签,记住要设置字体:
1 2 3 4 5 6 7 8 | <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-includes-a-custom-font/
尝试使用此链接http://www.barebonescoder.com/2010/05/android-development-using-custom-fonts/
将要定位的控件的typeface属性设置为衬线...,对于我建议使用TTF的字体文件,它过去对我有用。
也请尝试以下链接
http://techdroid.kbeanie.com/2011/04/using-custom-fonts-on-android.html
Android-使用自定义字体