Android 如何创建自定义文本视图?

Android 如何创建自定义文本视图?,android,textview,Android,Textview,我正在尝试创建一个自定义文本视图,该视图的字体设置来自给定路径。请为我提供任何示例,以及我如何用更少的代码实现这一点: <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/accountInfoText" android:textColor="#72

我正在尝试创建一个自定义文本视图,该视图的字体设置来自给定路径。请为我提供任何示例,以及我如何用更少的代码实现这一点:

<TextView
   android:id="@+id/textView2"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/accountInfoText"
   android:textColor="#727272"
   android:textSize="18dp" />

和xml格式:

<com.util.FontTextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/accountInfoText"
                    android:textColor="#727272"
                    android:textSize="18dp" />

文本视图创建自定义视图

  • attrs.xml
    文件中输入,并在自定义
    TextView
    中提供选择字体作为列表的选项

    ....
  • 使用字体列表创建枚举项并指定唯一值

  • strings.xml中输入所有字体

    机器人黑体 机器人培养基 机器人灯 机器人正规 机器人瘦 机械斜体

  • 创建一个资产文件夹,并复制要放入该文件夹的所有必要字体

  • 创建一个扩展
    TextView的类

    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Typeface;
    import android.util.AttributeSet;
    import android.widget.TextView;
    
    /**
    * Created by ANKIT 
    */
    public class CustomFontTextView extends TextView {
    
    String customFont;
    
    public CustomFontTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        style(context, attrs);
    }
    
    public CustomFontTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        style(context, attrs);
    
    }
    
    private void style(Context context, AttributeSet attrs) {
    
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.CustomFontTextView);
        int cf = a.getInteger(R.styleable.CustomFontTextView_fontName, 0);
        int fontName = 0;
        switch (cf)
        {
            case 1:
                fontName = R.string.Roboto_Bold;
                break;
            case 2:
                fontName = R.string.Roboto_Italic;
                break;
            case 3:
                fontName = R.string.Roboto_Light;
                break;
            case 4:
                fontName = R.string.Roboto_Medium;
                break;
            case 5:
                fontName = R.string.Roboto_Regular;
                break;
            case 6:
                fontName = R.string.Roboto_Thin;
                break;
            default:
                fontName = R.string.Roboto_Regular;
                break;
        }
    
        customFont = getResources().getString(fontName);
    
        Typeface tf = Typeface.createFromAsset(context.getAssets(),
                "font/" + customFont + ".ttf");
        setTypeface(tf);
        a.recycle();
    }
    }
    
  • 您可以这样使用此自定义类。。使用您的packageName.ClassName

     <ankit.com.customui.CustomFontTextView
      android:layout_width="match_parent"
      android:text="Hello World Ankit"
      android:textSize="16sp"
      app:fontName="Roboto_Medium"
      android:layout_height="wrap_content"/>
    
    
    
    reference我建议使用单例应用字体,以避免每次使用资产创建字体。@JackMahoney+1作为非常有用的注释。现在我的申请很快@JackMahoney您有没有一个将自定义视图转换为singleton以显示的示例?可能会有帮助。@kabuto178这里有一些伪代码,让您了解在这里使用AppCompatTextView更好,如果不使用它,您甚至会收到警告,实现完全相同,只需扩展AppCompatTextView而不是TextView。比公认的答案好得多的实现。
     <ankit.com.customui.CustomFontTextView
      android:layout_width="match_parent"
      android:text="Hello World Ankit"
      android:textSize="16sp"
      app:fontName="Roboto_Medium"
      android:layout_height="wrap_content"/>