Android 如何向字体文本视图类添加多个字体?
我有一个关于向textview添加多个自定义字体的问题。 我基本上在fonts文件夹中添加了字体,并基于我在网上找到的解决方案为fonttextview创建了一个java类。然而,我看到他们只添加了一种字体,我想添加多种字体,如roboto regular、roboto bold、cabin bold等。以下是我迄今为止的代码:Android 如何向字体文本视图类添加多个字体?,android,android-layout,android-intent,android-emulator,android-listview,Android,Android Layout,Android Intent,Android Emulator,Android Listview,我有一个关于向textview添加多个自定义字体的问题。 我基本上在fonts文件夹中添加了字体,并基于我在网上找到的解决方案为fonttextview创建了一个java类。然而,我看到他们只添加了一种字体,我想添加多种字体,如roboto regular、roboto bold、cabin bold等。以下是我迄今为止的代码: public class FontTextView extends TextView { public FontTextView(Context conte
public class FontTextView extends TextView {
public FontTextView(Context context) {
super(context);
Typeface face=Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Bold.ttf");
this.setTypeface(face);
}
public FontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
Typeface face=Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Bold.ttf");
this.setTypeface(face);
}
public FontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
Typeface face=Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Bold.ttf");
this.setTypeface(face);
}
如何创建多种字体?另外,我尝试了styleable等,但它显示了错误,因为它不支持styleable类,有人可以在现有代码中添加其他字体并引导我完成检索过程吗
谢谢!贾斯汀我建议在你的文本中使用HTML,这样你就可以使用不同的颜色/类型脸/ 看看: 一个有趣的解决方案是编码字体跨度:
public class CustomTypefaceSpan extends TypefaceSpan {
请看您可以在文本中编写html样式并使用
textView.setText(Html.fromHtml(displayString));
要添加颜色的示例字符串
String displayString = " <p style=\"color:#B4009E;\">Your string </p>" ;
String displayString=“您的字符串
”;
这就是我们如何在textView中创建html样式的方法。对于xml文件中设置的不同字体,请使用以下代码
public class CustomTextView extends TextView {
private static final String TAG = "CustomTextView";
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, attrs);
}
public CustomTextView(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.CustomTextView);
String customFont = a.getString(R.styleable.CustomTextView_customFont);
setCustomFont(ctx, customFont);
a.recycle();
}
public boolean setCustomFont(Context ctx, String asset) {
Typeface tf = null;
try {
tf = Typeface.createFromAsset(ctx.getAssets(), "fonts/"+asset);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface: "+e.getMessage());
return false;
}
setTypeface(tf);
return true;
}
}
在xml文件中,您可以将其用作:
<com.package_name.CustomTextView
your_name:customFont="arialbd.ttf" />
记住在values文件夹中添加attrs.xml,其中包含以下resource
<resources>
<declare-styleable name="CustomTextView">
<attr name="customFont" format="string"/>
</declare-styleable>
希望它有帮助你想同时使用不同的字体吗?我想使用一些if-else构造来使用特定的字体,但在同一个类中定义它。另外,我想通过我的xml文件中的“id”来调用if-else构造中的特定字体。我已经发布了一个答案,我想这就是你要问的。谢谢,快速提问,当您提到main时,您能告诉我如何解决可设置样式的问题,以及在哪里包括xlmns吗?对于添加的attrs.xml资源文件,我是否必须特别添加字体名称及其通用类型,如您所示?“string”xlmns将添加到顶部布局中,其中有
xmlns:android=”http://schemas.android.com/apk/res/android“
,就在那之后。那没用吗?我想在现有代码中设置自定义字体,例如:Typeface=Typeface.createFromAsset(context.getAssets(),“font/Roboto Bold.ttf”);这个。设置字体(面);Typeface face1=Typeface.createFromAsset(context.getAssets(),“font/Roboto Medium.ttf”);此设置字体(face1);用一些if-else循环和一个id通过我的xml文件调用它?我该怎么做呢?有什么线索吗?如果你想在同一个文本视图上使用多种字体,请看:谢谢你的帮助。我添加了上述代码,但是我看到styleable出现了一个错误,单击“quickfix”时显示了以下内容:链接本地重命名的所有引用(不会更改其他文件中的引用),它要求我重命名它。我该怎么做?尝试清理您的项目。不,我在customtextview中遇到一个错误,它不支持可样式化。如果存在导入Import android.R.*代码>,删除它。只有:import android.R;我认为这是必要的,一旦我删除它,它就会显示一个请求导入R的错误
<resources>
<declare-styleable name="CustomTextView">
<attr name="customFont" format="string"/>
</declare-styleable>