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

Android为自定义TextView小部件设置字体,第1张

概述我写了简单的小部件作为textview与一些属性,我想在扩展的TextView类中设置字体,如何做这个动作,我可以有这种能力?atributes:<resources><declare-styleablename="TextViewStyle"><attrname="selected_background"format="integer"/><att

我写了简单的小部件作为textvIEw与一些属性,我想在扩展的TextVIEw类中设置字体,如何做这个动作,我可以有这种能力?

atributes:

<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 =“”来设置资产中的任何字体

解决方法:

CustomTextVIEw:

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) {    }

FontManager:

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小部件设置字体全部内容,希望文章能够帮你解决Android为自定义TextView小部件设置字体所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1115661.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存