如何从XML设置的Andr​​oid字样 [英] how to set Android Typeface from xml

查看:210
本文介绍了如何从XML设置的Andr​​oid字样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 字样fontRobo = Typeface.createFromAsset(context.getAssets(),字体/的Roboto-Black.ttf);
viewTotalValue.setText(total.toString());


解决方案

您可以通过重写的TextView这样创建自己的TextView:

 公共类MyTextView扩展的TextView {    公共MyTextView(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
        超(背景下,ATTRS,defStyle);
        的setType(上下文);
    }    公共MyTextView(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
        的setType(上下文);
    }    公共MyTextView(上下文的背景下){
        超级(上下文);
        的setType(上下文);
    }    私人无效的setType(上下文的背景下){
        this.setTypeface(Typeface.createFromAsset(context.getAssets(),
                    foo.ttf));        this.setShadowLayer(1.5F,5,5,的getContext()getResources()的getColor(R.color.black_shadow));
    }
}

和使用这样的:

 < com.your.project.package.MyTextView
        机器人:ID =@ + ID / oppinfo_mtv_name
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_gravity =底
        机器人:文字=玩家1
        />

Typeface fontRobo = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Black.ttf");
viewTotalValue.setText(total.toString());

解决方案

You could create your own TextView by overriding the TextView like this:

public class MyTextView extends TextView {

    public MyTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setType(context);
    }

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setType(context);
    }

    public MyTextView(Context context) {
        super(context);
        setType(context);
    }

    private void setType(Context context){
        this.setTypeface(Typeface.createFromAsset(context.getAssets(),
                    "foo.ttf"));

        this.setShadowLayer(1.5f, 5, 5, getContext().getResources().getColor(R.color.black_shadow));
    }
}

And use it like this:

<com.your.project.package.MyTextView
        android:id="@+id/oppinfo_mtv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"  
        android:text="Player 1"
        />

这篇关于如何从XML设置的Andr​​oid字样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆