自定义view 详解

参考文章
非著名程序员http://www.gcssloop.com/#blog
抛物线http://hencoder.com/ui-1-1/
鸿洋http://blog.csdn.net/lmj623565791/article/details/24252901/

基本步奏

1、自定义View的属性
2、在View的构造方法中获得我们自定义的属性
[ 3、重写onMesure ]
4、重写onDraw

attrs.xml文件参考

format是值该属性的取值类型:
一共有:string,color,demension,integer,enum,reference,float,boolean,fraction,flag;

  <?xml version="1.0" encoding="utf-8"?>  
<resources>      
    <attr name="test" format="integer"/>  
    <declare-styleable name="customview">  
        <attr name="test"/> <!-- 复用声明在外部的属性定义test  -->  
          
        <attr name="atr1" format="reference"/> <!-- 参考引用某个资源 如@drawable/img-->  
        <attr name="atr2" format="string"/> <!-- 属性为string类型 -->  
        <attr name="atr3" format="string|reference"/> <!--  string类型或引用 -->  
        <attr name="atr4" format="boolean"/> <!-- 布尔型 true false  -->  
        <attr name="atr5" format="integer"/> <!-- 整数 -->  
        <attr name="atr6" format="float"/> <!-- 浮点  -->  
        <attr name="atr7" format="color"/> <!-- 颜色值 #rgb  #rrggbb #argb #aarrggbb -->  
        <attr name="atr8" format="dimension"/> <!-- 尺寸值 -->  
        <attr name="atr9" format="fraction"/> <!-- 百分比 -->  
        <attr name="atr10"> <!-- enum -->  
            <enum name="spring" value="1"/>  
            <enum name="summer" value="2"/>  
        </attr>  
        <attr name="art11"> <!-- 位或运算 表示 spring|summber  -->  
            <flag name="spring" value="4"/>  
            <flag name="summer" value="8"/>  
        </attr>  
    </declare-styleable>      
</resources>  
 
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容