高级UI<第三十七篇>:自定义View之Attrs

在自定义view中常常会用到自定义属性

(1)在项目中对应目录下新建attrs文件,用来新建自定义属性


比如:

<resources>

    <declare-styleable name="CustomView">
        <!--颜色值-->
        <attr name="textColor" format="color"/>
        <!--字符串-->
        <attr name="text" format="string"/>
        <!--尺寸-->
        <attr name="size" format="dimension"/>
        <!--布尔值-->
        <attr name="isBold" format="boolean"/>
        <!--枚举值-->
        <attr name="orientation" format="enum">
            <enum name="vertical" value="0"/>
            <enum name="horizontal" value="1"/>
        </attr>
        <!--位或运算-->
        <attr name="status">
            <flag name = "statusA" value = "1" />
            <flag name = "statusB" value = "3" />
            <flag name = "statusC" value = "5" />
        </attr>
        <!--浮点值-->
        <attr name="scalex" format="float"/>
        <!--参考某一资源ID-->
        <attr name="background" format="reference"/>
        <!--百分比-->
        <attr name="fraction" format="fraction"/>
        <!--整形-->
        <attr name="integer" format="integer"/>
    </declare-styleable>

</resources>

所有的属性上面已全部给出,大概有10种属性。

declare-styleable标签的name一般写自定义View的类型,如下:

<declare-styleable name="CustomView">

</declare-styleable>

当然也可以命名成其它名称。

attr标签表示定义一个属性,看一下下面的代码:

    <attr name="text" format="string"/>

这句代码的意思是,定义一个名称为text,类型为string的属性。

接下来需要完善一下代码,如下:

public class CustomView extends View {

    //将要填写的内容
    private String content = "我是中国人!";
    private int background;//背景
    private Paint mPaint;

    public CustomView(Context context) {
        this(context,null);
        //super(context);
    }

    public CustomView(Context context, @Nullable AttributeSet attrs) {
        this(context,attrs, 0);
        //super(context, attrs);
    }

    public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint();
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomView, defStyleAttr, 0);

        //获取颜色值
        int textColor = a.getColor(R.styleable.CustomView_textColor, Color.GRAY);
        //如果xml中没有设置颜色值,就使用默认,否则就使用xml中设置的颜色值
        mPaint.setColor(textColor);

        //获取字符串
        content = a.getString(R.styleable.CustomView_text);

        //获取尺寸
        int size = a.getDimensionPixelSize(R.styleable.CustomView_size, 18);
        mPaint.setTextSize(size);

        //获取boolean类型
        boolean isBold = a.getBoolean(R.styleable.CustomView_isBold, false);
        if(isBold){
            mPaint.setTypeface(Typeface.DEFAULT_BOLD);
        }else{
            mPaint.setTypeface(Typeface.DEFAULT);
        }

        //获取枚举数据
        String orientation = a.getString(R.styleable.CustomView_orientation);
        if("0".equals(orientation)){
        }else if("1".equals(orientation)){
        }

        //获取状态值
        int status = a.getInteger(R.styleable.CustomView_status, 0);
        if(status == 1){
            //如果是statusA
        }else if(status == 3){
            //如果是statusB
        }else if(size == 5){
            //如果是statusC
        }else if(size == 4){
            //如果是statusA且statusB
        }else if(size == 6){
            //如果是statusA且statusC
        }else if(size == 8){
            //如果是statusB且statusC
        }

        //获取浮点数
        float scalex = a.getFloat(R.styleable.CustomView_scalex, 1.0f);
        mPaint.setTextScaleX(scalex);

        //获取背景-参考某一资源ID
        background = a.getColor(R.styleable.CustomView_background, Color.BLUE);

        //获取百分比
        float fraction = a.getFraction(R.styleable.CustomView_fraction, 0, 0, 0);
        mPaint.setTextScaleX(fraction);

        //获取整形
        int inte = a.getInteger(R.styleable.CustomView_integer, 0);

        a.recycle();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(background);
        canvas.drawText(content, 100, 100, mPaint);
    }

代码中都写明了用法,他们的意思是:获取布局中设置的值,如果布局中没有设置,则为默认值

布局的用法如下:

<com.zyc.hezuo.attrsdemo.CustomView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:textColor="#000000"
    app:text="我是帅气的中国人!"
    app:size="30sp"
    app:isBold="true"
    app:orientation="horizontal"
    app:status="statusA|statusB"
    app:scalex="0.7"
    app:fraction="0.3%"
    app:background="@color/colorAccent"/>

注意事项:
(1)getIndexCount

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomView, defStyleAttr, 0);
        int count = a.getIndexCount();

这里的count就是xml布局中被使用的属性个数,那么,请往下看,如果xml代码是这样的,请问,count的值是多少?

<com.zyc.anidemo.CustomView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:textColor="#000000"
    app:text="我是帅气的中国人!"
    app:size="30sp"
    app:isBold="true"
    app:orientation="horizontal"
    app:status="statusA|statusB"
    app:scalex="0.7"
    app:fraction="0.3%"
    app:background="@color/colorAccent"/>

答案是9;

如果xml代码是这样的

<com.zyc.anidemo.CustomView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:fraction="0.3%"
    app:background="@color/colorAccent"/>

那么,count的值是2;

什么时候count的值为0呢?那肯定是没有使用xml属性的情况,如下

<com.zyc.anidemo.CustomView
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

像这种情况count取值为0;

[本章完...]

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,542评论 6 504
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,822评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,912评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,449评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,500评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,370评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,193评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,074评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,505评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,722评论 3 335
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,841评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,569评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,168评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,783评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,918评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,962评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,781评论 2 354

推荐阅读更多精彩内容