对于实现自定义字体,我在这里看到几个例子问题是不同的,我在一个抽象类中使用自定义字体,这是在所有的应用程序中使用。我无法更改字体。 在此先感谢。如何在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