【问题标题】:Multiple fonts to a single custom TextView多个字体到单个自定义 TextView
【发布时间】:2018-07-27 05:28:01
【问题描述】:

我们如何在单个自定义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 描述了另一种方法,但这在内存方面存在问题。正在寻找一个好的解决方案。

【问题讨论】:

  • 您必须以编程方式进行。在xml中你只能声明一次。

标签: android xml fonts textview


【解决方案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" />
    

瞧!

【讨论】:

    猜你喜欢
    • 2016-08-11
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2013-08-23
    相关资源
    最近更新 更多