近期在捣鼓android 自己定义控件属性,学到了TypedArray以及attrs。在这当中看了一篇大神博客Android
深入理解Android中的自己定义属性
。我就更加深入学习力一番。我就沿着这个学习,讲一下流程吧,兴许一篇还有应用。

1、attrs文件编写

<?xml version="1.0" encoding="utf-8"?>
<resources> <attr name="titleText" format="string" />
<attr name="titleTextColor" format="color" />
<attr name="titleTextSize" format="dimension" /> <declare-styleable name="AuthCodeView">
<attr name="titleText" />
<attr name="titleTextColor" />
<attr name="titleTextSize" />
</declare-styleable> </resources>

看到这上面的代码有三个属性,首先attr标签是定义名字以及属性。后面是一个declare-styleable组,这个组名字AuthCodeView,后面class中会用到。

2、在xml里面怎么引用以及使用。对照系统空间属性

先看两张图。就了解大半了,也理解大半了。

a、自己定义属性的名字的引用

b、细致看图上说明以及a跟b图的比較。你就知道属性名改变。以及怎么引用。

怕上面图片看不清。附上部分xml代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:authcodeview="http://schemas.android.com/apk/res/com.example.authcodeview"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <com.example.authcodeview.view.AuthCodeView
android:id="@+id/AuthCodeView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
authcodeview:titleText="3712"
authcodeview:titleTextColor="#00ffff"
authcodeview:titleTextSize="40sp" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击验证码,换一张" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="输入验证码" /> <EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" > <requestFocus />
</EditText>
</LinearLayout> <Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="验证" /> </LinearLayout>

重点看头部layout中xmlns:android="http://schemas.android.com/apk/res/android"这是引用系统属性的作用。

然而 xmlns:authcodeview="http://schemas.android.com/apk/res/com.example.authcodeview"是引用自己定义属性。

xmlns:+名称 = "http://schemas.android.com/apk/res/ + 应用的包名"

后面使用时候自己定义属性就是这样啦

authcodeview:titleText="3712"

            authcodeview:titleTextColor="#00ffff"

            authcodeview:titleTextSize="40sp"

顺便附上系统arrs自己定义的路径

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

3、在自己定义控件中class怎么引用问题了

看一段代码先

	/**
* 获得我自己定义的样式属性
*
* @param context
* @param attrs
* @param defStyle
*/
public AuthCodeView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
/**
* 获得我们所定义的自己定义样式属性
*/
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.AuthCodeView, defStyle, 0); //获取在attr文件下,名字为AuthCodeView的declare-styleable属性有几个
int n = a.getIndexCount();
for (int i = 0; i < n; i++)
{
int attr = a.getIndex(i);
switch (attr)
{
//这个属性能够不要,由于都是随机产生
case R.styleable.AuthCodeView_titleText:
mTitleText = a.getString(attr);
break;
case R.styleable.AuthCodeView_titleTextColor:
// 默认颜色设置为黑色
mTitleTextColor = a.getColor(attr, Color.BLACK);
break;
case R.styleable.AuthCodeView_titleTextSize:
// 默认设置为16sp。TypeValue也能够把sp转化为px
mTitleTextSize = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
break; } }
a.recycle();
}

这个TypedArray的作用就是资源的映射作用。写法是这种。

R.styleable.AuthCodeView这个是不是非常熟悉。

还有R.styleable.AuthCodeView_titleText,后面就是名称加上下横线加上属性。

这样做就把自己定义属性在xml设置值映射到class,怎么获取都非常easy。

有空格看以去看看生成id的R文件。

这篇先到这里结束,还有这篇的续集。自己定义属性控件。也是自己定义view。随机验证码demo学习。

android 自己定义控件属性(TypedArray以及attrs解释)的更多相关文章

  1. Android自己定义控件皮肤

    Android自己定义控件皮肤 对于Android的自带控件,其外观仅仅能说中规中矩,而我们平时所示Android应用中,一个简单的button都做得十分美观.甚至于很多button在按下时的外观都有 ...

  2. Android自己定义控件:进度条的四种实现方式

    前三种实现方式代码出自: http://stormzhang.com/openandroid/2013/11/15/android-custom-loading/ (源代码下载)http://down ...

  3. android 自己定义控件

    Android自己定义View实现非常easy 继承View,重写构造函数.onDraw.(onMeasure)等函数. 假设自己定义的View须要有自己定义的属性.须要在values下建立attrs ...

  4. Android自己定义控件系列五:自己定义绚丽水波纹效果

    尊重原创!转载请注明出处:http://blog.csdn.net/cyp331203/article/details/41114551 今天我们来利用Android自己定义控件实现一个比較有趣的效果 ...

  5. Android自己定义控件之应用程序首页轮播图

    如今基本上大多数的Android应用程序的首页都有轮播图.就是像下图这种(此图为转载的一篇博文中的图.拿来直接用了): 像这种组件我相信大多数的应用程序都会使用到,本文就是自己定义一个这种组件,能够动 ...

  6. Android自己定义控件系列案例【五】

    案例效果: 案例分析: 在开发银行相关client的时候或者开发在线支付相关client的时候常常要求用户绑定银行卡,当中银行卡号一般须要空格分隔显示.最常见的就是每4位数以空格进行分隔.以方便用户实 ...

  7. Android自己定义控件2-简单的写字板控件

    概述 上一篇文章我们对自己定义控件进行了一个大体的知识介绍. 今天就来学习自己定义一个简单的写字板控件. 先来看看效果图 就是简单的依据手指写下的轨迹去画出内容 实现 在上一篇文章里提到了androi ...

  8. Android自己定义控件(状态提示图表)

    [工匠若水 http://blog.csdn.net/yanbober 转载烦请注明出处.尊重分享成果] 1 背景 前面分析那么多系统源代码了.也该暂停下来歇息一下,趁昨晚闲着看见一个有意思的需求就操 ...

  9. Android自己定义控件系列二:自己定义开关button(一)

    这一次我们将会实现一个完整纯粹的自己定义控件,而不是像之前的组合控件一样.拿系统的控件来实现.计划分为三部分:自己定义控件的基本部分,自己定义控件的触摸事件的处理和自己定义控件的自己定义属性: 以下就 ...

随机推荐

  1. 修改MySQL默认字符集

    今天发现有库级字符集和表级字符集,实验了下发现,库级字符集是该库内表的默认字符集,当创建表时,如果未指定字符集,默认使用该表所属库的字符集.表也可使用不同于所属库的字符集. MySQL对于字符集的指定 ...

  2. SQL2008所有数据导出导入两种方法

    方法一:生成脚本导出导入sql2008所有数据 第一步.右键要导出的数据库.任务--生成脚本 第二步,在设置脚本编写选项处,点击--高级(A),选择要编写脚本的数据的类型为:架构和数据 假设找不到 要 ...

  3. 关于Windows7下创建Cocos2D-X项目的小问题

    "新版的Cocos2D-X"已经不支持用上述脚本来创建工程了,而是改为用create-project.py来创建...命令格式: python create-project.py ...

  4. CentOS6 安装 aria2

    CentOS6 安装 aria2 https://www.jianshu.com/p/31ea7aba5524 http://blog.51cto.com/skypegnu1/1637168 1.先安 ...

  5. Vsftp权限控制(持续增加中)

    把用户限制在自己的home目录中,例如限制用户Leon只能访问/home/Leon目录下的文件,不允许访问上级目录. 先打开配置文件 vi /etc/vsftpd/vsftpd.conf 第一种方法: ...

  6. 深入理解Android(5)——从MediaScanner分析Android中的JNI

    前面几篇介绍了Android中的JNI和基本用法,这一篇我们通过分析Android源代码中的JNI实例,来对JNI部分做一个总结. 一.通向两个不同世界的桥梁 在前面我们说过,JNI就像一个桥梁,将J ...

  7. Web 端 js 导出csv文件

    http://www.qdfuns.com/notes/35821/2ab249182734d1f5c66da6b5cf395db9.html

  8. Ubuntu16.04安装官方Firefox 火狐浏览器 延长支持版(Extended Support Release, 简称“ESR”)

    Ubuntu16.04安装官方Firefox 火狐浏览器 延长支持版(Extended Support Release, 简称“ESR”) 延长支持版本(Extended Support Releas ...

  9. https soap链接示例

    1.先安装soap扩展sudo yum install php-soap 2.安装openssL 3.function  issure($sn){//通过soap链接接口  进行确认是否是正确的sn码 ...

  10. OpenJDK源码研究笔记(十一):浅析Javac编译过程中的抽象语法树(IfElse,While,Switch等语句的抽象和封装)

    浅析OpenJDK源码编译器Javac的语法树包com.sun.source.tree. 抽象语法树,是编译原理中的经典问题,有点难,本文只是随便写写. 0.赋值语句 public interface ...