多个字体到单个自定义TextView

时间:2018-02-16 10:15:21

标签: android xml fonts textview

我们如何在单个自定义TextView中使用不同风格的Roboto(或任何其他)字体(Roboto-Regular,Roboto-Thin,Roboto-Bold等)。

here描述了以编程方式执行此操作的方法之一。但是如果我们想要使用XML来改变样式,如何实现这一点。

可以使用像这样的自定义属性来完成吗?

<com.project.abc.customviews.XYZTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome"
        app:type="thin" />

我不想为每种类型制作不同的java文件。另一种方法是here,但这有关于记忆的问题。寻找一个好的解决方案。

1 个答案:

答案 0 :(得分:-1)

这就是我的成就:

  1. 创建自定义TextView

    public class CaptainTextView extends TextView {
    private HashMap<String, Typeface> mTypefaces;
    
    public CaptainTextView(Context context) {
        super(context);
    }
    
    public CaptainTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    super(context, attrs, defStyleAttr);
    if (mTypefaces == null) {
        mTypefaces = new HashMap<>();
    }
    
    if (this.isInEditMode()) {
        return;
    }
    
    final TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CaptainTextView);
    if (array != null) {
        final String typefaceAssetPath = array.getString(
            R.styleable.CaptainTextView_customTypeface);
    
        if (typefaceAssetPath != null) {
            Typeface typeface;
    
            if (mTypefaces.containsKey(typefaceAssetPath)) {
                typeface = mTypefaces.get(typefaceAssetPath);
            } else {
                AssetManager assets = context.getAssets();
                typeface = Typeface.createFromAsset(assets, typefaceAssetPath);
                mTypefaces.put(typefaceAssetPath, typeface);
            }
    
            setTypeface(typeface);
        }
        array.recycle();
    }
    }
    
    public CaptainTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        if (mTypefaces == null) {
            mTypefaces = new HashMap<>();
        }
    
        if (this.isInEditMode()) {
            return;
        }
    
        final TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CaptainTextView);
        if (array != null) {
            final String typefaceAssetPath = array.getString(
                R.styleable.CaptainTextView_customTypeface);
    
            if (typefaceAssetPath != null) {
                Typeface typeface;
    
                if (mTypefaces.containsKey(typefaceAssetPath)) {
                    typeface = mTypefaces.get(typefaceAssetPath);
                } else {
                    AssetManager assets = context.getAssets();
                    typeface = Typeface.createFromAsset(assets, typefaceAssetPath);
                    mTypefaces.put(typefaceAssetPath, typeface);
                }
    
                setTypeface(typeface);
            }
            array.recycle();
        }
    }
    }
    
  2. 声明了自定义属性

    <resources>
    <declare-styleable name="CaptainTextView">
        <attr name="customTypeface" format="string" />
    </declare-styleable>
    </resources>
    
  3. 在XML中使用

    <com.project.captain.customviews.CaptainTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome"
        android:textSize="16sp"
        android:textStyle="bold"
        app:customTypeface="fonts/Roboto-Thin.ttf" />
    
  4. 瞧!