【问题标题】:How do I add multiple fonts to font text view class?如何将多种字体添加到字体文本视图类?
【发布时间】:2013-05-30 22:00:37
【问题描述】:

我有一个关于向 textview 添加多个自定义字体的问题。 我基本上已经在字体文件夹中添加了字体,并根据我在网上找到的解决方案为 fonttextview 创建了一个 java 类。但是我看到他们只添加了一种字体,我想添加多种字体,如roboto-regular、roboto-bold、cabin-bold 等。这是我到目前为止的代码:

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 类,任何人都可以在现有代码中添加另一种字体并引导我完成检索过程吗?

谢谢!贾斯汀

【问题讨论】:

  • 你想同时使用不同的字体??
  • 我想使用一些 if else 构造来使用特定的字体,但在同一个类中定义它。另外,我想通过我的 xml 文件中的“id”来调用 if else 构造中的分词字体
  • 我已经发布了一个答案,我想这就是你要问的。
  • 谢谢,快速提问,你能告诉我如何解决样式问题,以及当你提到 main 和添加 attrs.xml 的资源文件时在哪里包含 xlmns 我是否必须特别需要如您所见,添加字体名称及其通用类型? “字符串”
  • xlmns 将被添加到有xmlns:android="http://schemas.android.com/apk/res/android" 的顶部布局中,紧随其后。

标签: android android-layout android-intent android-emulator android-listview


【解决方案1】:

使用以下代码为 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" />

并在主父布局中添加

xmlns:your_name="http://schemas.android.com/apk/res/com.package_name"

记得在 values 文件夹中添加 attrs.xml,并在其中添加 resource

<resources>
<declare-styleable name="CustomTextView">
    <attr name="customFont" format="string"/>
</declare-styleable>

希望对你有帮助

【讨论】:

  • 感谢您的帮助。我添加了上面的代码,但是我看到一个样式错误,单击“quickfix”时显示以下内容:链接本地重命名的所有引用(不更改引用其他文件),它要求我重命名它。我该怎么办?
  • 尝试清理你的项目。
  • 不,我在不支持样式的 customtextview 中收到错误。
  • 如果有 Import import android.R.*;,删除它。
  • 只有:import android.R;我认为这是必要的,一旦我删除它,它就会显示一个错误,要求导入 R
【解决方案2】:

我建议在您的文本中使用 HTML,以便您可以使用不同的颜色/字体/...

看看:

Html in text view with different fonts for bold and italic

Is it possible to have multiple styles inside a TextView?

Using size HTML attribute in TextView

一个有趣的解决方案是编写字体跨度:

 public class CustomTypefaceSpan extends TypefaceSpan {

How can I use TypefaceSpan or StyleSpan with a custom Typeface?

【讨论】:

  • 那没有帮助?我想在现有代码中设置自定义字体,例如:Typeface face=Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Bold.ttf"); this.setTypeface(face);字体 face1=Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Medium.ttf"); this.setTypeface(face1);有一些 if else 循环和一个 id 来通过我的 xml 文件调用它?我该怎么做?有什么线索吗?
  • 如果您打算在同一个 TextView 上使用多种字体,请查看:stackoverflow.com/questions/4819049/…
【解决方案3】:

可以在文本中编写html样式并使用

textView.setText(Html.fromHtml(displayString));

添加颜色的示例字符串

String displayString = " <p style=\"color:#B4009E;\">Your string </p>" ;

这就是我们如何在 textView 中做 html 样式

【讨论】:

    猜你喜欢
    • 2011-01-27
    • 2019-01-24
    • 2023-01-31
    • 1970-01-01
    • 2015-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-05
    相关资源
    最近更新 更多