自定义控件

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

2020-01-22  本文已影响0人  NoBugException

在自定义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;

[本章完...]

上一篇 下一篇

猜你喜欢

热点阅读