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