1 自定义属性值

自定义view的起步是自定义属性,并且正确的读取属性。
在res/values/attrs.xml的文件中创建属性:

   <declare-styleable name="ViewDemoAttr35"><attr name="string35" format="string"/><attr name="boolean35" format="boolean"/><attr name="color35"  format="color"/><attr name="integer35" format="integer"/><attr name="enum35" format="enum"><enum name="enum0" value="0" /><enum name="enum1" value="1" /><enum name="enum2" value="2" /></attr><attr name="dimension35" format="dimension"/><attr name="float35" format="float"/><attr name="reference351" format="reference"/><attr name="reference352" format="reference"/><attr name="reference353" format="reference"/><attr name="reference354" format="reference"/><attr name="fraction35" format="fraction"/><attr name="bg351" format="color|reference"/><attr name="bg352" format="color|reference"/><attr name="flag35" ><flag name="flag1" value="1"/><flag name="flag2" value="2"/></attr></declare-styleable>

或者:

<!--属性声明-->
<attr name="demo1" format="string"/>
<!--属性使用-->
<declare-styleable name="MyTextView"><attr name="demo1"/>
</declare-styleable>

两种方式略有不同,第一种方式定义的属性只能在内部使用,第二种方式定义的属性作为公共属性,其他自定view也可以使用。

自定义属性需要name和format,name是属性的名字,format是属性的格式:
Android支持的属性格式包括:

  • string 属性取值为string,或者指向R.string.id 的一个资源
  • color 属性取值为颜色,类似#cdcdcd,或者指向R.color.id
  • boolean 属性取值为true或者false
  • dimension 属性取值为尺寸类型,例如px,sp,dp也可以指向一个R.dimen.id
  • enum 属性指向具体的枚举值
  • flag 属性指向一个flag类型,位运算,类似enum,
  • float 属性指向一个float
  • fraction 属性指向一个百分数,只能是xx%样式的数据
  • integer 属性指向一个int值
  • reference 属性指向一个引用id,类似R.drawable.id

注意:
attr节点有两种子节点,enum和flag,enum和flag不能混合使用。
enum可在format声明,也可以不声明,当attr节点内有enum节点时,attr可以赋值为enum。
flag不可在format声明,当attr节点内有flag节点时,attr可以赋值为flag。
enum子节点的声明,value的值只能是int类型,且只能有一个值。
flag的声明,value也只能是int,但是可以利用|设置多个值。

还可以定义为 color|reference 一般用于颜色 @color/。

2 读取属性

自定义View需要实现三个构造函数(最好),属性就存储在attrs中,利用Context的obtainStyledAttributes方法得到TypedArray 对象进行属性获取,最后需要调用TypedArray 的recycle方法进行回收。

public ViewDemo35(Context context) {this(context,null);
}public ViewDemo35(Context context, @Nullable AttributeSet attrs) {this(context, attrs,0);
}public ViewDemo35(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);handleCustomAttrs(context,attrs);init();
}private void handleCustomAttrs(Context context,AttributeSet attrs) {if (attrs == null) {return;}TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ViewDemoAttr35);String str = typedArray.getString(R.styleable.ViewDemoAttr35_string35);boolean boo = typedArray.getBoolean(R.styleable.ViewDemoAttr35_boolean35, false);int color1 = typedArray.getColor(R.styleable.ViewDemoAttr35_color35, Color.BLUE);int intege = typedArray.getInteger(R.styleable.ViewDemoAttr35_integer35, 10);int enum1 = typedArray.getInt(R.styleable.ViewDemoAttr35_enum35, 1);int size1 = typedArray.getDimensionPixelSize(R.styleable.ViewDemoAttr35_dimension35, 22);float float1 = typedArray.getFloat(R.styleable.ViewDemoAttr35_float35, 22);int resid1 = typedArray.getResourceId(R.styleable.ViewDemoAttr35_reference351, R.drawable.close_icon);int resid2 = typedArray.getResourceId(R.styleable.ViewDemoAttr35_reference352, R.color.black);Drawable drawable1 = typedArray.getDrawable(R.styleable.ViewDemoAttr35_reference353);int resid3 = typedArray.getResourceId(R.styleable.ViewDemoAttr35_reference354, R.drawable.compositedst1);float fraction = typedArray.getFraction(R.styleable.ViewDemoAttr35_fraction35,1,1,0.1f);int color2 = typedArray.getColor(R.styleable.ViewDemoAttr35_bg351,R.drawable.gradientdemo);int color3 =  typedArray.getColor(R.styleable.ViewDemoAttr35_bg352,R.drawable.background_tab);int int4 = typedArray.getInt(R.styleable.ViewDemoAttr35_flag35, 1);typedArray.recycle();
}
}

typeArray的getXXX函数的第一个参数组成styleable的name+_+属性name,例如.styleable.ViewDemoAttr35_fraction35,ViewDemoAttr35是styleable的名字,fraction35是属性名。

3如何使用

AS 下,布局文件根目录工具推荐的方法,只写一个命名空间
xmlns:app="http://schemas.android.com/apk/res-auto,就可以包含所有的自定义属性,利用app:的前缀就可以使用所有的自定义属性了。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".Main15Activity2"><com.ldx.canvasdrawdemo.LinearLayoutDemoandroid:id="@+id/view_group_demo"android:layout_width="match_parent"android:layout_height="100dp"android:orientation="vertical"><TextViewandroid:id="@+id/demo_tv1"android:layout_width="match_parent"android:layout_height="50dp"android:text="sjldjflsjfljs"/></com.ldx.canvasdrawdemo.LinearLayoutDemo><com.ldx.canvasdrawdemo.ViewDemo35android:layout_width="match_parent"android:layout_height="100dp"app:string35="string35"app:boolean35="true"app:color35="#cdcdcd"app:integer35="22"app:enum35="enum0"app:dimension35="25dp"app:float35="22"app:reference351 = "@drawable/ic_launcher_background"app:reference352 = "@drawable/shape_deep_blue"app:reference353 = "@drawable/have_icon"app:reference354 = "@drawable/image_home_game_nor2"app:fraction35 = "20%"app:bg351 = "@color/colorPrimary"app:bg352 = "@color/black"app:flag35 = "flag1" /></android.support.constraint.ConstraintLayout>

Android自定义View 之自定义属性相关推荐

  1. 精通Android自定义View(五)自定义属性值使用详情

    1 可查看Android自定义View的基本使用 1 精通Android自定义View(一)自定义控的基本使用 2 精通Android自定义View(二)自定义属性使用详解 2 string 字符串 ...

  2. Android 自定义View,自定义属性--自定义圆形进度条(整理)

    很多的时候,系统自带的View满足不了我们的功能需求,那么我们就需要自定义View来满足我们的需求 自定义View时要先继承View,添加类的构造方法,重写父类View的一些方法,例如onDraw,为 ...

  3. 精通Android自定义View(四)自定义属性使用详解

    1.简述 对于自定义属性,遵循以下几步,就可以实现: 自定义一个CustomView(extends View )类 编写values/attrs.xml,在其中编写styleable和item等标签 ...

  4. 精通Android自定义View(十四)绘制水平向右加载的进度条

    1引言 1 精通Android自定义View(一)View的绘制流程简述 2 精通Android自定义View(二)View绘制三部曲 3 精通Android自定义View(三)View绘制三部曲综合 ...

  5. 精通Android自定义View(十二)绘制圆形进度条

    1 绘图基础简析 1 精通Android自定义View(一)View的绘制流程简述 2 精通Android自定义View(二)View绘制三部曲 3 精通Android自定义View(三)View绘制 ...

  6. Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)

    转载请注明地址:http://blog.csdn.net/xiaanming/article/details/10298163 很多的时候,系统自带的View满足不了我们功能的需求,那么我们就需要自己 ...

  7. Android自定义view详解,使用实例,自定义属性,贝塞尔曲线

    //只会触发执行onDraw方法,只会改变绘制里面的内容,条目的绘制 invalidate(); //只会触发执行onDraw方法,但是可以在子线程中刷新 postInvalidate(); //vi ...

  8. Android自定义View:ViewGroup(三)

    自定义ViewGroup本质是什么? 自定义ViewGroup本质上就干一件事--layout. layout 我们知道ViewGroup是一个组合View,它与普通的基本View(只要不是ViewG ...

  9. android自定义抽奖,Android自定义view制作抽奖转盘

    本文实例为大家分享了Android自定义view制作抽奖转盘的具体代码,供大家参考,具体内容如下 效果图 TurntableActivity package com.bawei.myapplicati ...

最新文章

  1. 认识RESTful设计风格
  2. 梯度下降法的三种形式BGD、SGD以及MBGD
  3. matlab新手入门(四)(翻译)
  4. 支付宝招兼职“找茬程序员” 不用坐班/最高奖励36万
  5. linux 查看内存用量_正确计算linux系统内存使用率
  6. mac安装vue-cli脚手架;脚手架安装报错Error: EACCES: permission denied, access ‘/usr/local/lib/node_modules
  7. 这个开源组织里的项目都是精品
  8. 首批5G成员!中兴天机Axon 10 Pro下周发布
  9. Android adb “push pull”中文支持解决方案
  10. 20155307 刘浩 信息安全技术(李冬冬) 实验三 数字证书应用 实验报告
  11. 你抢购盐干什么?要抢购也是先选大米啊
  12. Svn内外网切换技巧
  13. deepstream-test3
  14. 怎样解决“在禁用UAC时,无法激活此应用”问题
  15. linux dm9000驱动分析,ARM-Linux驱动--DM9000网卡驱动分析(二)
  16. h5页面使用html2canvas实现分享海报
  17. 【python】pickle
  18. Shader学习笔记(三)学习Shader所需的数学基础
  19. outlook自定义快捷键_如何自定义主题和Outlook邮件的格式
  20. H5 兼容底部地址栏、搜索栏

热门文章

  1. 咒术师学院spellcaster university for mac
  2. Nmap学习——目标主机、端口、操作系统和服务探测以及输出格式
  3. 由《英雄联盟》引发的思考
  4. HTML <head> 头部
  5. cmake是什么意思
  6. 微型计算机用什么显卡,《绝地求生:大逃杀》全系主流显卡大型评测之显卡需求测试...
  7. cad怎么卸载干净_怎么卸载干净office?附卸载工具+教程方法
  8. 反恐精英服务器维护到几点,CSOL4月22日更新维护公告 停机维护到几点
  9. EA使用小技巧-定制类图的显示方式
  10. 初识C——《猜数游戏》