zoukankan      html  css  js  c++  java
  • android自定义控件(三) 自定义属性

    书接上回 

    在xml里建立属性,然后java代码里用typedArray获得这些属性,得到属性后,利用属性做一些事.例:得到xml里的color,赋给paint.

    1.在res/values/下新建attrs.xml

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <resources>  
    3.     <declare-styleable name="CustomView2">  
    4.         <attr name="textColor" format="color" />  
    5.         <attr name="textSize" format="dimension" />  
    6.     </declare-styleable>  
    7. </resources>  
    8. <!-- name="CustomView1"控件名称 得到TypedArray时用 -->  
    9. <!-- name="textColor" 对应test:textColor -->  
    10. <!-- format="color" 对应构造方法里a.getColor(R.styleable.CustomView2_textColor, 0xFFFFFFFF); -->  

    format详解可参照http://blog.csdn.net/ethan_xue/article/details/7315064
    2.主要看构造函数

    1. public class CustomView2 extends View {  
    2.   
    3.     private Paint mPaint2;  
    4.     private String mText = "drawText";  
    5.   
    6.     public CustomView2(Context context, AttributeSet attrs) {  
    7.         super(context, attrs);  
    8.         mPaint2 = new Paint();  
    9.         // TypedArray是存放资源的array,1.通过上下文得到这个数组,attrs是构造函数传进来的,对应attrs.xml  
    10.         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomView2);  
    11.         // 获得xml里定义的属性,格式为 名称_属性名 后面是默认值  
    12.         int textColor = a.getColor(R.styleable.CustomView2_textColor, 0xFFFFFFFF);  
    13.         float textSize = a.getDimension(R.styleable.CustomView2_textSize, 35);  
    14.         mPaint2.setColor(textColor);  
    15.         mPaint2.setTextSize(textSize);  
    16.         // 为了保持以后使用该属性一致性,返回一个绑定资源结束的信号给资源  
    17.         a.recycle();  
    18.     }  
    19.   
    20.     @Override  
    21.     protected void onDraw(Canvas canvas) {  
    22.         super.onDraw(canvas);  
    23.   
    24.         mPaint2.setStyle(Style.FILL);  
    25.         canvas.drawText(mText, 10, 60, mPaint2);  
    26.     }  
    27.   
    28. }  

    3.布局

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <!-- xmlns:test="http://schemas.android.com/apk/res/ethan.customview1" 包名 -->  
    3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    4.     xmlns:test="http://schemas.android.com/apk/res/ethan.customview1"   
    5.     android:orientation="vertical"  
    6.     android:layout_width="fill_parent"  
    7.     android:layout_height="fill_parent"  
    8.     >  
    9. <ethan.customview1.CustomView2    
    10.     android:layout_width="wrap_content"   
    11.     android:layout_height="wrap_content"   
    12.     test:textColor="#f00"  
    13.     test:textSize="20sp"  
    14.     />  
    15. </LinearLayout>  

    4.效果图

    下载地址 http://download.csdn.net/detail/ethan_xue/4108832

  • 相关阅读:
    JAVA常见面试题之Forward和Redirect的区别
    [转载]vm文件
    [转载]vm文件
    [转载]心灵丨愿你早一点发现,你才是自己最重要的粉丝
    [转载]心灵丨愿你早一点发现,你才是自己最重要的粉丝
    iBatis整理——Spring环境下批处理实现
    iBatis整理——Spring环境下批处理实现
    SAP HANA 三大特点
    在解决方案中搜索 文件的位置
    我这边测了一下,发现#后面参数变化浏览器不会刷新,但是#一旦去掉就会刷新了,你那边的url拼的时候能不能在没参数的时候#也拼在里面,这样应该就OK了
  • 原文地址:https://www.cnblogs.com/chengzhengfu/p/4574086.html
Copyright © 2011-2022 走看看