【发布时间】:2012-02-14 06:31:54
【问题描述】:
您好,我正在编写一个安卓应用程序。而且无论手机的版本如何,我都想要其中的机器人字体。有办法吗?
谢谢, 拉希姆。
【问题讨论】:
您好,我正在编写一个安卓应用程序。而且无论手机的版本如何,我都想要其中的机器人字体。有办法吗?
谢谢, 拉希姆。
【问题讨论】:
是的,为什么不呢,你可以得到 Roboto 字体:
假设您想更改文本视图的字体:
Typeface tf = Typeface.createFromAsset(getAssets(),
"fonts/Roboto-Black.ttf");
TextView tv = (TextView) findViewById(R.id.FontTextView);
tv.setTypeface(tf);
【讨论】:
试试这个链接http://www.barebonescoder.com/2010/05/android-development-using-custom-fonts/
将您要定位的控件的字体属性设置为衬线...对于我推荐使用 TTF 的字体文件,它过去对我有用
也可以试试这些链接
http://techdroid.kbeanie.com/2011/04/using-custom-fonts-on-android.html
【讨论】:
在 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/
【讨论】: