自定义控件之自定义属性——随笔

为什么要有自定义属性,因为android中自带的属性不能满足我们的需求了,所以需要自定义属性。

  第一步:在res/values文件夹下添加一个attrs.xml文件,如果项目比较大,会导致attrs.xml代码比较多,可以根据相应的功能模块起名字,方便查找,如:登录相关的模块 attrs_login.xml

  <?xml version="1.0" encoding="utf-8"?>
  <resources>
    <!--声明属性-->
  </resources>
  创建完成是这个样子

  第二步:文件创建完成怎么声明属性

  使用<declare-styleable name="CustomCilcles"></declare-styleable> 来定义一个属性集合,name对应的就是属性集合的名字,这个名字最好是见名之意。

 

  <?xml version="1.0" encoding="utf-8"?>
  <resources>
   <declare-styleable name="CustomCircles">
  <!--添加属性-->
   </declare-styleable>
  </resources>

  第三步:定义属性,通过<attr name="textColor" format="color"/>方式来定义属性值,属性名字最好也是见名之意,format表示这个属性的值的类型。有如下几种属性:

 

  • reference:引用资源

  • string:字符串

  • Color:颜色

  • boolean:布尔值

  • dimension:尺寸值

  • float:浮点型

  • integer:整型

  • fraction:百分数

  • enum:枚举类型

  • flag:位或运算

    例如我们随笔定义几个属性:圆的半径,圆的背景颜色,进度的背景颜色,进度大小

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <declare-styleable name="CustomCircles">
    <attr name="circle_radius" format="integer"/>
    <attr name="circle_progress" format="integer"/>
    <attr name="circle_backgroundcolor" format="color"/>
    <attr name="circle_progresscolor" format="color"/>
    </declare-styleable>
    </resources>

    第四步: 怎么去使用这些属性了,  先到xml布局文件中去

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:qiaoning="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.kallaite.rxjavademo.customcontrols.CustomCircles
    android:layout_width="150dp"
    android:layout_height="150dp"
    qiao:circle_progress="360"
    qiao:circle_radius="50"
    qiao:circle_backgroundcolor="@android:color/holo_orange_dark"
    qiao:circle_progresscolor="@android:color/holo_red_dark"
    />
    </LinearLayout>

为属性集设置一个属性集名称,我这里用的qiao,这个可以随便起,建议在真正的项目中使用项目的缩写,比如微信可能就是使用wx。

  第五步: 自定义空间中,如何获取自定义属性

  每一个集合编译之后都会对应一个styleable对象,通过styleable对象获取TypedArray,然后通过键值对获取到属性值

private void init(Context context, AttributeSet attrs) {

TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomCircles);    //R.styleable.CustomCircles  这对应的是attrs.xml文件中定义的属性名字
backgroundColor = typedArray.getColor(R.styleable.CustomCircles_circle_backgroundcolor, Color.RED);
progressColor = typedArray.getColor(R.styleable.CustomCircles_circle_progresscolor, Color.BLACK);
circleProgress = typedArray.getInteger(R.styleable.CustomCircles_circle_progress, 20);
int circleRadius = typedArray.getInteger(R.styleable.CustomCircles_circle_radius, 40);

}

attrs是怎么来的呢? 还记得构造方法吗?  从第一个属性开始就有attrs这个参数,需要传递过来使用, 一个参数的构造方法传null就可以了

public CustomCircles(Context context) {
super(context);
init(context, null);
}

public CustomCircles(Context context, AttributeSet attrs) {
super(context, attrs);
init(context,attrs);
}

public CustomCircles(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}

通过上述步骤就可以拿到我们在xml布局文件中定义的属性值,在init()方法中取得就可以拿去使用了。

 

posted @ 2017-06-22 10:29  wlwqnj  阅读(210)  评论(0编辑  收藏  举报