步驟1)創建一個類名CustomTextView並複製粘貼此代碼,但確保自定義字體應該是Project Assets文件夾。所以你可以替換字體名稱。
包YOUR_PACKAGE;
public class CustomTextView extends TextView{
public CustomButton(Context context) {
super(context);
setFont();
}
public CustomTextView (Context context, AttributeSet attrs) {
super(context, attrs);
setFont();
}
public CustomTextView (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setFont();
}
private void setFont() {
Typeface normal = Typeface.createFromAsset(getContext().getAssets(),"fonts/Arial.ttf");
setTypeface(normal, Typeface.NORMAL);
}
}
步驟2)
使用該自定義你的佈局的TextView
<LinearLayout
android:id="@+id/signing_details"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="100" >
<YOUR_PACKAGE.CustomTextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textSize="16sp" />
</ LinearLayout>
您也可以使用此CustomTextView動態
CustomTextView textView = new CustomTextView (this);
textView.setText(res.getString(R.string.app_name));
textView.setTextColor(Color.parseColor("#000000"));
textView.setTextSize(16);
希望這有助於你。