android 自己定义控件属性(TypedArray以及attrs解释)
近期在捣鼓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解释)的更多相关文章
- Android自己定义控件皮肤
Android自己定义控件皮肤 对于Android的自带控件,其外观仅仅能说中规中矩,而我们平时所示Android应用中,一个简单的button都做得十分美观.甚至于很多button在按下时的外观都有 ...
- Android自己定义控件:进度条的四种实现方式
前三种实现方式代码出自: http://stormzhang.com/openandroid/2013/11/15/android-custom-loading/ (源代码下载)http://down ...
- android 自己定义控件
Android自己定义View实现非常easy 继承View,重写构造函数.onDraw.(onMeasure)等函数. 假设自己定义的View须要有自己定义的属性.须要在values下建立attrs ...
- Android自己定义控件系列五:自己定义绚丽水波纹效果
尊重原创!转载请注明出处:http://blog.csdn.net/cyp331203/article/details/41114551 今天我们来利用Android自己定义控件实现一个比較有趣的效果 ...
- Android自己定义控件之应用程序首页轮播图
如今基本上大多数的Android应用程序的首页都有轮播图.就是像下图这种(此图为转载的一篇博文中的图.拿来直接用了): 像这种组件我相信大多数的应用程序都会使用到,本文就是自己定义一个这种组件,能够动 ...
- Android自己定义控件系列案例【五】
案例效果: 案例分析: 在开发银行相关client的时候或者开发在线支付相关client的时候常常要求用户绑定银行卡,当中银行卡号一般须要空格分隔显示.最常见的就是每4位数以空格进行分隔.以方便用户实 ...
- Android自己定义控件2-简单的写字板控件
概述 上一篇文章我们对自己定义控件进行了一个大体的知识介绍. 今天就来学习自己定义一个简单的写字板控件. 先来看看效果图 就是简单的依据手指写下的轨迹去画出内容 实现 在上一篇文章里提到了androi ...
- Android自己定义控件(状态提示图表)
[工匠若水 http://blog.csdn.net/yanbober 转载烦请注明出处.尊重分享成果] 1 背景 前面分析那么多系统源代码了.也该暂停下来歇息一下,趁昨晚闲着看见一个有意思的需求就操 ...
- Android自己定义控件系列二:自己定义开关button(一)
这一次我们将会实现一个完整纯粹的自己定义控件,而不是像之前的组合控件一样.拿系统的控件来实现.计划分为三部分:自己定义控件的基本部分,自己定义控件的触摸事件的处理和自己定义控件的自己定义属性: 以下就 ...
随机推荐
- 关于git及其github的使用
一:序言(就是瞎扯) 人们都说不会使用git和github的程序员都不是好程序员,是的,当我第一次听到的时候有点失望.因为我也不会...但是这句话激起了我学习使用git的动力(其实也没怎么深入的学习) ...
- 紫书 例题 10-22 UVa 1640(数位统计)
这道题的题解有几个亮点 一个是每次只统计一个数字来简化思维 一个是统计当前位数的时候分三个部分来更新答案 具体看代码,有注释 #include<cstdio> #include<cs ...
- Linux Network配置
/etc/sysconfig/network [root@mytest ~]# cat /etc/sysconfig/network NETWORKING=yes HOSTNAME=mytest /e ...
- PKU 2528 Mayor's posters
题意: 一个公告板上面贴海报,宽度都是一样的,长度可能不一样.后面的海报可能把前面的覆盖掉.问最后能看见多少张不同的海报. 思路: 这题原来做过,是用线段树的区间染色写的.记录每个区间是纯色还是杂色. ...
- C# http服务器
Http 服务器搭建 1.新建一个C#控制台工程 2.复制以下代码 using System; using System.Collections.Generic; using System.Linq; ...
- Java实现断点续传。
http://www.cnblogs.com/liaojie970/p/5013790.html
- Java.Lang.NoSuchMethod 错误
项目开发.调用webservice,方法调用报了 Java.Lang.NoSucheMethod..........,印象中记得是jar包冲突,maven项目,一看,这一堆jar包...用eclips ...
- 推荐一款优雅的jquery手风琴特效
推荐一款基于 jQuery和 CSS3实现的手风琴效果. 查看demo演示
- SharePoint创建Alternate Access Mapping (AAM)备用訪问映射
SharePoint创建Alternate Access Mapping (AAM)备用訪问映射 SharePoint的仓库是SQL Server中的内容数据库.这些数据库储存着组织全 ...
- Python数据可视化——散点图
PS: 翻了翻草稿箱. 发现竟然存了一篇去年2月的文章...尽管naive.还是发出来吧... 本文记录了python中的数据可视化--散点图scatter, 令x作为数据(50个点,每一个30维), ...