Android 为自定义 TextView 小部件设置字体

标签 android android-widget

我将简单的小部件编写为具有某些属性的 TextView ,我想在扩展的 TextView 类中为其设置字体,如何执行此操作并且我可以拥有此功能?

属性:

<resources>
    <declare-styleable name="TextViewStyle">
       <attr name="selected_background" format="integer" />
       <attr name="font"                format="string" />
    </declare-styleable>
</resources>

自定义 TextView 小部件:

public class TextViewStyle extends TextView{

    public TextViewStyle(Context context) {
        super(context);
    }
    public TextViewStyle(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TextViewStyle(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TextViewStyle, defStyle, 0);

        a.recycle();
    }
}

将简单的 UI 小部件转换为 xml:

<ir.jaziire.widgets.TextViewStyle
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/topic"
    android:textColor="#000"
    android:textSize="14dp"
    android:gravity="right"
    />

在这个小部件中,我想设置 app:font="" 以设置 Assets 中的任何字体

最佳答案

自定义 TextView :

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.TextView;

import com.androidhub.R;

public class CustomTextView extends TextView {

    public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        try {
            TypedArray a = context.obtainStyledAttributes(attrs,
                    R.styleable.font, defStyle, 0);

            String str = a.getString(R.styleable.font_fonttype);
            a.recycle();
            switch (Integer.parseInt(str)) {
            case 0:
                str = "fonts/Trebuchet_MS.ttf";
                break;
            default:
                break;
            }

            setTypeface(FontManager.getInstance(getContext()).loadFont(str));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public CustomTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @SuppressWarnings("unused")
    private void internalInit(Context context, AttributeSet attrs) {

    }

字体管理器:

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.graphics.Typeface;

public class FontManager {

    private Map<String, Typeface> fontCache = new HashMap<String, Typeface>();
    private static FontManager instance = null;
    private Context mContext;

    private FontManager(Context mContext2) {
        mContext = mContext2;
    }

    public synchronized static FontManager getInstance(Context mContext) {

        if (instance == null) {
            instance = new FontManager(mContext);
        }
        return instance;
    }

    public Typeface loadFont(String font) {

        if (false == fontCache.containsKey(font)) {
            fontCache.put(font,
                    Typeface.createFromAsset(mContext.getAssets(), font));
        }
        return fontCache.get(font);
    }
}

attrs.xml 文件中:

<declare-styleable name="font">
        <attr name="fonttype">
            <enum name="trebuchet_ms" value="0" />
        </attr>
    </declare-styleable>

在您的 xml 中使用您的 CustomTextView 作为:

在您的 xml 顶部声明它

xmlns:custom="http://schemas.android.com/apk/res/com.androidhub"

并且您可以将 CustomTextView 用作:

<com.utils.CustomTextView
                android:id="@+id/loadMap"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:background="@drawable/custom_button_selector"
                android:clickable="true"
                android:ellipsize="marquee"
                android:fadingEdge="horizontal"
                android:gravity="center"
                android:marqueeRepeatLimit="marquee_forever"
                android:padding="10dp"
                android:scrollHorizontally="true"
                android:singleLine="true"
                android:text="@string/load_map"
                android:textColor="@color/home_buttons_selector"
                android:textSize="16sp"
                custom:fonttype="trebuchet_ms" />

确保您将字体放在 assests-->fonts-->YourFont

com.androidhub 是我的包名。

关于Android 为自定义 TextView 小部件设置字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28362023/

相关文章:

java - 字符串在 Android 上的 Java 中似乎不相等,即使它们打印相同

android - EditText 未显示在 Ice Cream Sandwich 下

android - 如何获取已经在 J​​SONArray 中的 JSONArray

android - Android 上的解析文件

android - 使用 LinearLayout 的自定义小部件没有得到 onDraw()

android - 尝试检索 LinearLayout 时出现奇怪的 NullpointerException

java - 如何从android中的两个数组列表制作地理编码

android - 升级 Google Play Services 7.5.0 后 RatingBar 颜色显示为白色

android - 在Android中正确定位弹出窗口

android EditText maxLength 允许用户输入更多