對於實現自定義字體,我在這裏看到幾個例子問題是不同的,我在一個抽象類中使用自定義字體,這是在所有的應用程序中使用。我無法更改字體。 在此先感謝。如何在textview中使用自定義字體android
-1
A
回答
1
嘗試一下本作自定義字體弗朗資產
// Font path
String fontPath = "fonts/Face Your Fears.ttf";
// text view label
TextView txtGhost = (TextView) findViewById(R.id.ghost);
// Loading Font Face
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
// Applying font
txtGhost.setTypeface(tf);
的更多信息,請此, http://www.androidhive.info/2012/02/android-using-external-fonts/
2
你可以這樣做。 你要添加的文件夾資產
ArialMTBoldRegularTextView.java您.TFF文件:
public final class ArialMTBoldRegularTextView extends CustomTextView {
public static final String FONT_PATH = "arial-rounded-mt-bold.ttf";
public ArialMTBoldRegularTextView(Context context) {
super(context);
setFont(FONT_PATH);
}
public ArialMTBoldRegularTextView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
setFont(FONT_PATH);
}
public ArialMTBoldRegularTextView(Context context, AttributeSet attributeSet, int defStyleAttr) {
super(context, attributeSet, defStyleAttr);
setFont(FONT_PATH);
}
public void setFont(String fontPath) {
changeFont(this, fontPath);
}
public static void changeFont(final CompoundButton button, final String fontPath) {
Typeface typeface = Typeface.createFromAsset(button.getContext().getAssets(), fontPath);
button.setTypeface(typeface);
}
}
CustomTextView.java
public class CustomTextView extends TextView {
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
public CustomTextView(Context context, AttributeSet attributeSet, int defStyleAttr) {
super(context, attributeSet, defStyleAttr);
}
public Typeface getFont(final Context context, final String fontPath) {
return Typeface.createFromAsset(context.getAssets(), fontPath);
}
public void changeFont(final TextView textView, final String fontPath) {
Typeface typeface = Typeface.createFromAsset(textView.getContext().getAssets(), fontPath);
textView.setTypeface(typeface);
}
public void changeFont(final CompoundButton button, final String fontPath) {
Typeface typeface = Typeface.createFromAsset(button.getContext().getAssets(), fontPath);
button.setTypeface(typeface);
}
}
和fater以xml:
<packagename.views.ArialMTBoldRegularTextView
android:layout_width="wrap_content"
android:layout_height="35dp"
android:gravity="center"
android:text="Pseudo"/>
4
像下面創建自定義類。
public class CustomTextView extends TextView {
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public CustomTextView(Context context) {
super(context);
init(null);
}
private void init(AttributeSet attrs) {
if (attrs!=null) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CustomTextView);
String fontName = a.getString(R.styleable.CustomTextView_fontName);
if (fontName!=null) {
Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/"+fontName);
setTypeface(myTypeface);
}
a.recycle();
}
}
}
並將您的字體添加到assets> fonts文件夾中。
加入attrs.xml
<declare-styleable name="CustomTextView">
<attr name="fontName" format="string" />
</declare-styleable>
使用以下。
<com.abc.cusomclass.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fontName="/*name of your font from assets/font folder*/"/>
1
現在支持Android庫26個支持直接使用的字體從您的XML看到doc瞭解更多詳情。
相關問題
- 1. 自定義字體和自定義的TextView在Android
- 2. Android - 如何使用自定義的TextView?
- 3. 使用自定義字體的自定義textview
- 4. Android - 將自定義字體應用到textview中不起作用
- 5. Android:當我使用自定義字體時,TextView被複制
- 6. 如何在自定義BaseAdapter中使用自定義字體
- 7. 如何在android應用程序中使用自定義字體?
- 8. ArrayAdapter中textview的自定義字體
- 9. 如何在android庫項目中使用自定義字體?
- 10. 如何在android xml中使用自定義字體?
- 11. 如何在Xamarin.Android中使用自定義字體?字體問題
- 12. 在Android中使用TextView,TextView,RadioGroup自定義ListView
- 13. 擴展TextView中的自定義字體? Android的
- 14. 如何在android textview中使用文本對齊方式並同時使用自定義字體樣式?
- 15. 時鐘textview的自定義字體
- 16. 如何使用CSS自定義字體?
- 17. 如何使用自定義CSS字體?
- 18. 如何使用自定義的省略號Android中的TextView
- 19. 如何使用textview自定義uialertview?
- 20. 如何在日曆中應用自定義字體android
- 21. 在UIWebView中使用自定義字體
- 22. 在css中使用自定義字體?
- 23. 在Cocos2d中使用自定義字體
- 24. 在CSS中使用自定義字體
- 25. 在xaml中使用自定義字體
- 26. 在Xcode中使用自定義字體
- 27. 在CSS中使用自定義字體
- 28. 在viewpagerindicator中使用自定義字體
- 29. 如何在可搜索字典中使用自定義字體?
- 30. 如何在CKEditor中自定義字體?
檢查https://futurestud.io/tutorials/custom-fonts-on-android-extending-text-view – nnn