自定义属性的过程

    xiaoxiao2021-12-13  33

    1.新建XML文件attrs

    2. 例如

    <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyView"> <attr name="maxNum" format="integer"/> <attr name="startAngle" format="integer"/> </declare-styleable> </resources>

    3.format 对应的类别

    reference:参考某一资源id,类似于平常用的@drawable/...

    color:颜色值

    boolean:布尔值

    dimension:尺寸值(如果这里是dp那就会做像素转换)

    float:浮点值

    integer:整型值

    string:字符串

    fraction:百分数

    enum:枚举值

    flag:自己定义的,类似于android:gravity = “top”;

    reference | color :颜色的资源文件

    reference | boolean :布尔值的资源文件

    4.代码中获取引用

    private void initAttr(AttributeSet attrs) { TypedArray array = getContext().obtainStyledAttributes(attrs,R.styleable.CreditScoreIndicatorView); maxNum = array.getInt(R.styleable.CreditScoreIndicatorView_maxNum,500); startAngle = array.getInt(R.styleable.CreditScoreIndicatorView_startAngle,160); array.recycle(); }

    转载请注明原文地址: https://ju.6miu.com/read-950070.html

    最新回复(0)