我已将自定义字体文件添加到我的assets/fonts文件夹中.如何从我的XML中使用它?
我可以从代码中使用它如下:
TextView text = (TextView) findViewById(R.id.textview03);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Molot.otf");
text.setTypeface(tf);
Run Code Online (Sandbox Code Playgroud)
我不能使用android:typeface="/fonts/Molot.otf"属性从XML做到吗?
我有一个应用程序,可以更改某些元素的字体字体.它适用于大多数人,但在尝试更改字体时可能会有0.5%的异常.堆栈跟踪的重要部分是:
Caused by: java.lang.RuntimeException: native typeface cannot be made
at android.graphics.Typeface.<init>(Typeface.java:147)
at android.graphics.Typeface.createFromAsset(Typeface.java:121)
Run Code Online (Sandbox Code Playgroud)
正如我所说,它适用于大多数人,所以我不认为这是字体文件或我的代码的问题.有关如何解决这个问题的任何建议?
编辑:这是我的代码:
Typeface phoneticFont = Typeface.createFromAsset(getAssets(),
"fonts/CharisSILR.ttf");
TextView tv;
tv = ((TextView) findViewById(R.id.searchPronunciationTitle));
tv.setTypeface(phoneticFont);
Run Code Online (Sandbox Code Playgroud) 以下用于设置自定义字体的代码会降低整个应用程序的速度.我如何修改它以避免内存泄漏,提高速度和管理内存?
public class FontTextView extends TextView {
private static final String TAG = "FontTextView";
public FontTextView(Context context) {
super(context);
}
public FontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, attrs);
}
public FontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context, attrs);
}
private void setCustomFont(Context ctx, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.FontTextView);
String customFont = a.getString(R.styleable.FontTextView_customFont);
setCustomFont(ctx, customFont);
a.recycle();
}
public boolean setCustomFont(Context ctx, String asset) {
Typeface tf = null;
try {
tf = …Run Code Online (Sandbox Code Playgroud) android android-emulator android-intent android-layout android-listview
有没有办法在Android中更改EditText的字体?我希望它匹配我为所有textViews设置的字体.
我有一个最低API级别为14的应用程序.我认为所有兼容设备都应该安装Roboto字体作为默认设置吗?如果我将textView字体设置为Roboto或Roboto Light,它似乎默认为普通的sans字体.
有没有办法使用Roboto而不包括Roboto字体作为资产?
我试图在一个自定义字体上实现EditText.有没有人有更好的方法,而不是我现在正在做的事情?
Typeface myFont = Typeface.createFromAsset(getAssets(), "fonts/myfont.ttf");
edittext.setTypeface(myFont);
Run Code Online (Sandbox Code Playgroud)
因为我有很多EditText...