您的位置:首页 > 移动开发 > Android开发

Android自定义控件——自定义属性

2016-11-14 02:15 344 查看
自定义属性的过程:

 1.在res/values文件夹中创建attrs的xml文件。

 2.写入<declare-styleable >标签, 定义子标签attr,放入自定义属性的名称。

format  可以用|来同时使用

1、reference   参考某一资源Id

2、color          颜色值

3、boolean     布尔值

4、dimension 尺寸值(带有单位的 sp/dp)

5、float           浮点型

6、intager 整形

7、string 字符串

8、fraction       百分比

9、enum 枚举

10、flag 位或运算

实例:

[java] view
plain copy

 print?





public class CustomView2 extends View{  

    private Paint paint;  

    private String text;  

    /** 

     * 初始化画笔 

     * */  

    private void initPaint(){  

        paint = new Paint();  

        paint.setColor(Color.BLACK);  

        paint.setAntiAlias(true);  //抗锯齿  

        setPadding(10,10,10,30);  

    }  

  

    public CustomView2(Context context, AttributeSet attrs) {  

        super(context, attrs);  

        initPaint();  

  

        /** 

         * 获取自定义属性的内容: 

         * 怎么把xml文件加载为java当中的对象 

         * View     LayoutInflater 

         * menu     MeunInflater 

         * animation   AnimationUtils 

         * animator    AnimatorInflater 

         *  attrs      TypeArray 

         * */  

        //属性类型数组  

        TypedArray typedArray = context.obtainStyledAttributes  

                (attrs, R.styleable.CustomView);  

        //得到自定义属性在xml布局当中的设置内容  

       String text = typedArray.getText  

               (R.styleable.CustomView_titleText).toString();  

        Log.i("tag","text====="+text);  

       setText(text);  

        int color = typedArray.getColor  

                (R.styleable.CustomView_titleColor,Color.BLACK);  

        Log.i("tag","color====="+color);  

        setColor(color);  

        float size = typedArray.getDimension  

                (R.styleable.CustomView_titleSize,15);  

        Log.i("tag","size===="+size);  

        setSize(size);  

  

        typedArray.recycle();   //清理资源,回收资源,为了防止下一次使用的时候造成影响  

  

    }  

  

    /** 

     *  设置文字内容 

     * */  

    public void setText(String text){  

        this.text = text;  

        requestLayout();    //重新绘制界面  

        invalidate();       //重新加载数据  

    }  

    /** 

     *  设置文字颜色 

     * 

     * */  

    public void setColor(int color){  

        paint.setColor(color);  

        requestLayout();  

        invalidate();  

    }  

  

    /** 

     * 设置文字的尺寸 

     * */  

    public  void setSize(float size){  

        paint.setTextSize(size);  

        requestLayout();  

        invalidate();  

    }  

    public CustomView2(Context context) {  

        super(context);  

    }  

  

    @Override  

    protected void onDraw(Canvas canvas) {  

        super.onDraw(canvas);  

        canvas.drawColor(Color.GRAY);  

  

        canvas.drawText(text,getPaddingLeft(),getPaddingTop()+(paint.descent()-paint.ascent()),paint);  

  

    }  

  

    @Override  

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);  

          int wSize = setMeasure(widthMeasureSpec,1);  

          int hSize = setMeasure(heightMeasureSpec,2);  

        setMeasuredDimension(wSize,hSize);  

    }  

  

  

    public int setMeasure(int measureSpec,int type){  

        int result = 0;  

        //获取测量的模式  

        int mode = MeasureSpec.getMode(measureSpec);  

        int size = MeasureSpec.getSize(measureSpec);   //允许控件的最大的值  

        switch (mode) {  

  

            case MeasureSpec.AT_MOST:  

                if (type==1){   //宽度  

//                    //paint.measureText(text)   :得出文字的长度  

                    result = (int) (getPaddingLeft()+getPaddingRight()+paint.measureText(text));  

  

                }else if(type==2){   //高度  

                    result = (int) (getPaddingBottom()+getPaddingTop()+(paint.descent()-paint.ascent()));  

                }  

                break;  

            case MeasureSpec.EXACTLY:  

                result = size;  

                break;  

        }  

        return  result;  

    }  

  

}  

[html] view
plain copy

 print?





<?xml version="1.0" encoding="utf-8"?>  

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  

    xmlns:tools="http://schemas.android.com/tools"  

    xmlns:cs="http://schemas.android.com/apk/res-auto"  

    android:layout_width="match_parent"  

    android:layout_height="match_parent"  

    tools:context="com.xinyuliu.custom.demo02.CustomActivity2">  

    <com.xinyuliu.custom.demo02.CustomView2  

        android:id="@+id/view2"  

        android:layout_height="wrap_content"  

        android:layout_width="wrap_content"  

        cs:titleText="我是一个大好人!!"  

        cs:titleSize="25sp"  

        cs:titleColor="#00ffff">  

  

    </com.xinyuliu.custom.demo02.CustomView2>  

</RelativeLayout>  

[html] view
plain copy

 print?





<?xml version="1.0" encoding="utf-8"?>  

<resources>  

    <!--declare-styleable 放置自定义属性的标签   name:便于在自定义view当中查找属性-->  

    <declare-styleable name="CustomView">  

        <!--创建自定义属性-->  

        <attr name="titleText" format="string|reference"></attr>  

        <attr name="titleSize" format="dimension|reference"></attr>  

        <attr name="titleColor" format="color|reference"></attr>  

    </declare-styleable>  

    <declare-styleable name="TitleView">  

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

         <attr name="mybuttontext" format="reference"></attr>  

    </declare-styleable>  

  

</resources>  

运行效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: