美文网首页
自定义属性步骤

自定义属性步骤

作者: StoneWay3 | 来源:发表于2019-04-28 11:32 被阅读0次

第一步:创建属性文件attrs.xml

位置:res -> values ->new xml ->attrs.xml

声明一个名字

声明具体的属性

属性值的类型简单介绍(format)

1.reference 引用类型值:@+id/……等

2.color 颜色类型值 #FFFFFF 

3.boolean 布尔类型 true和false 

4.dimension 尺寸类型值 dp/sp/dip/px/pt等

5.integer 整数类型值 progress max weight,R.color.xx等等

6.float 小数类型 0.1f,1.3f等

7.string 字符类型 "text" 等

8.<enum> 枚举类型 水平,垂直,居中等等

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

    <declare-styleable name="RoundProgressBar">

        <attr name="roundColor" format="color"/>

        <attr name="roundProBarColor" format="color"/>

        <attr name="textColor" format="color"/>

        <attr name="roundWidth" format="dimension"/>

        <attr name="textSize" format="dimension"/>

        <attr name="progress" format="integer"/>

        <attr name="proBarMax" format="integer"/>

</resources>

第二步:在布局文件中引用当前的命名空间

其中app是系统自动生成的,也可以随意自定义

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

第三步:在自定义控件中使用自定义的属性

app:roundColor="@color/colorPrimary"

第四步:在自定义控件的构造器中,引用自定义的属性

//获取自定义的属性

TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar);

//取出所有的自定义属性

roundColor = typedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.GRAY);

roundProBarColor = typedArray.getColor(R.styleable.RoundProgressBar_roundProBarColor, Color.RED);

textColor = typedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.RED);

roundWidth = typedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, UiUtils.dp2px(10));

textSize = typedArray.getDimension(R.styleable.RoundProgressBar_textSize, UiUtils.dp2px(20));

proBarMax = typedArray.getInteger(R.styleable.RoundProgressBar_proBarMax, 100);

progress = typedArray.getInteger(R.styleable.RoundProgressBar_progress, 33);

//回收处理

typedArray.recycle();

相关文章

  • 自定义属性

    阅读原文 自定义属性 自定义属性步骤 1_创建工程:05.自定义属性 2_创建属性类MyAttributeView...

  • Android_自定义属性

    自定义属性 1 自定义属性的基本步骤 a 自定义一个CustomView继承View b 在valus...

  • Android自定义View总结

    Android自定义View总结 [toc] 步骤 自定义View的属性 在View的构造方法获取我们自定义的属性...

  • 自定义View的简单流程

    自定义View的步骤 1、自定义属性 1)分析自定义的View中需要哪些的自定义属性,在res/values/at...

  • Android自定义View步骤

    自定义View 的步骤 自定义View属性 在View的构造方法中获得自定义的属性 重写onMesure 重写on...

  • Android日志:自定义属性和文本绘制

    概览 改变自定义View属性值得两种方式1、代码直接设置2、自定义属性,xml中配置自定义属性的步骤1、 创建at...

  • 安卓自定义 View 启航

    先总结下自定义 View 的步骤: 自定义 View 的属性 在 View 的构造方法中获得我们自定义的属性 [重...

  • Android 自定义View

    自定义View的步骤:自定义View的属性在View的构造方法中获得我们自定义View的步骤[3.重写onMeas...

  • 自定义ViewGroup的简单流程

    自定义ViewGroup的步骤 1、自定义属性 参考UI控件:自定义View 2、onMeasure() View...

  • Android三级联动wheel代码分析(二)

    自定义View的步骤:1、自定义View的属性2、在View的构造方法中获得我们自定义的属性[3、重写onMesu...

网友评论

      本文标题:自定义属性步骤

      本文链接:https://www.haomeiwen.com/subject/cdcdnqtx.html