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

来源:互联网 发布:部队网络安全管理 编辑:程序博客网 时间:2024/05/22 10:31
自定义属性的过程:
 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?在CODE上查看代码片派生到我的代码片
  1. public class CustomView2 extends View{  
  2.     private Paint paint;  
  3.     private String text;  
  4.     /** 
  5.      * 初始化画笔 
  6.      * */  
  7.     private void initPaint(){  
  8.         paint = new Paint();  
  9.         paint.setColor(Color.BLACK);  
  10.         paint.setAntiAlias(true);  //抗锯齿  
  11.         setPadding(10,10,10,30);  
  12.     }  
  13.   
  14.     public CustomView2(Context context, AttributeSet attrs) {  
  15.         super(context, attrs);  
  16.         initPaint();  
  17.   
  18.         /** 
  19.          * 获取自定义属性的内容: 
  20.          * 怎么把xml文件加载为java当中的对象 
  21.          * View     LayoutInflater 
  22.          * menu     MeunInflater 
  23.          * animation   AnimationUtils 
  24.          * animator    AnimatorInflater 
  25.          *  attrs      TypeArray 
  26.          * */  
  27.         //属性类型数组  
  28.         TypedArray typedArray = context.obtainStyledAttributes  
  29.                 (attrs, R.styleable.CustomView);  
  30.         //得到自定义属性在xml布局当中的设置内容  
  31.        String text = typedArray.getText  
  32.                (R.styleable.CustomView_titleText).toString();  
  33.         Log.i("tag","text====="+text);  
  34.        setText(text);  
  35.         int color = typedArray.getColor  
  36.                 (R.styleable.CustomView_titleColor,Color.BLACK);  
  37.         Log.i("tag","color====="+color);  
  38.         setColor(color);  
  39.         float size = typedArray.getDimension  
  40.                 (R.styleable.CustomView_titleSize,15);  
  41.         Log.i("tag","size===="+size);  
  42.         setSize(size);  
  43.   
  44.         typedArray.recycle();   //清理资源,回收资源,为了防止下一次使用的时候造成影响  
  45.   
  46.     }  
  47.   
  48.     /** 
  49.      *  设置文字内容 
  50.      * */  
  51.     public void setText(String text){  
  52.         this.text = text;  
  53.         requestLayout();    //重新绘制界面  
  54.         invalidate();       //重新加载数据  
  55.     }  
  56.     /** 
  57.      *  设置文字颜色 
  58.      * 
  59.      * */  
  60.     public void setColor(int color){  
  61.         paint.setColor(color);  
  62.         requestLayout();  
  63.         invalidate();  
  64.     }  
  65.   
  66.     /** 
  67.      * 设置文字的尺寸 
  68.      * */  
  69.     public  void setSize(float size){  
  70.         paint.setTextSize(size);  
  71.         requestLayout();  
  72.         invalidate();  
  73.     }  
  74.     public CustomView2(Context context) {  
  75.         super(context);  
  76.     }  
  77.   
  78.     @Override  
  79.     protected void onDraw(Canvas canvas) {  
  80.         super.onDraw(canvas);  
  81.         canvas.drawColor(Color.GRAY);  
  82.   
  83.         canvas.drawText(text,getPaddingLeft(),getPaddingTop()+(paint.descent()-paint.ascent()),paint);  
  84.   
  85.     }  
  86.   
  87.     @Override  
  88.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  89.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  90.           int wSize = setMeasure(widthMeasureSpec,1);  
  91.           int hSize = setMeasure(heightMeasureSpec,2);  
  92.         setMeasuredDimension(wSize,hSize);  
  93.     }  
  94.   
  95.   
  96.     public int setMeasure(int measureSpec,int type){  
  97.         int result = 0;  
  98.         //获取测量的模式  
  99.         int mode = MeasureSpec.getMode(measureSpec);  
  100.         int size = MeasureSpec.getSize(measureSpec);   //允许控件的最大的值  
  101.         switch (mode) {  
  102.   
  103.             case MeasureSpec.AT_MOST:  
  104.                 if (type==1){   //宽度  
  105. //                    //paint.measureText(text)   :得出文字的长度  
  106.                     result = (int) (getPaddingLeft()+getPaddingRight()+paint.measureText(text));  
  107.   
  108.                 }else if(type==2){   //高度  
  109.                     result = (int) (getPaddingBottom()+getPaddingTop()+(paint.descent()-paint.ascent()));  
  110.                 }  
  111.                 break;  
  112.             case MeasureSpec.EXACTLY:  
  113.                 result = size;  
  114.                 break;  
  115.         }  
  116.         return  result;  
  117.     }  
  118.   
  119. }  
[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     xmlns:cs="http://schemas.android.com/apk/res-auto"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="com.xinyuliu.custom.demo02.CustomActivity2">  
  8.     <com.xinyuliu.custom.demo02.CustomView2  
  9.         android:id="@+id/view2"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_width="wrap_content"  
  12.         cs:titleText="我是一个大好人!!"  
  13.         cs:titleSize="25sp"  
  14.         cs:titleColor="#00ffff">  
  15.   
  16.     </com.xinyuliu.custom.demo02.CustomView2>  
  17. </RelativeLayout>  
[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <!--declare-styleable 放置自定义属性的标签   name:便于在自定义view当中查找属性-->  
  4.     <declare-styleable name="CustomView">  
  5.         <!--创建自定义属性-->  
  6.         <attr name="titleText" format="string|reference"></attr>  
  7.         <attr name="titleSize" format="dimension|reference"></attr>  
  8.         <attr name="titleColor" format="color|reference"></attr>  
  9.     </declare-styleable>  
  10.     <declare-styleable name="TitleView">  
  11.          <attr name="mytitle" format="string"></attr>  
  12.          <attr name="mybuttontext" format="reference"></attr>  
  13.     </declare-styleable>  
  14.   
  15. </resources>  
运行效果:
0 0
原创粉丝点击