Android-AttributeSet详解
public interface AttributeSet {
/**
* Returns the number of attributes available in the set.
*
* @return A positive integer, or 0 if the set is empty.
*/
public int getAttributeCount();
/**
* Returns the name of the specified attribute.
*
* @param index Index of the desired attribute, 0...count-1.
*
* @return A String containing the name of the attribute, or null if the
* attribute cannot be found.
*/
public String getAttributeName(int index);
/**
* Returns the value of the specified attribute as a string representation.
*
* @param index Index of the desired attribute, 0...count-1.
*
* @return A String containing the value of the attribute, or null if the
* attribute cannot be found.
*/
public String getAttributeValue(int index);
/**
* Returns the value of the specified attribute as a string representation.
* The lookup is performed using the attribute name.
*
* @param namespace The namespace of the attribute to get the value from.
* @param name The name of the attribute to get the value from.
*
* @return A String containing the value of the attribute, or null if the
* attribute cannot be found.
*/
public String getAttributeValue(String namespace, String name);
查看AttributeSet的源码 你会发现它是一个接口 是个什么接口呢?
熟悉XML解析的人知道 在XML解析中是有AttributeSet这个东西的,XML解析根据节点取出节点相对应的数据。
Android中,我们写的布局文件就是XML形式的,所以这就是每次我们自定义View的时候,构造方法有AttributeSet的原因。
SDK给出的解释如下:
A collection of attributes, as found associated with a tag in an XML document. Often you will not want to use this interface directly, instead passing it to Resources.Theme.obtainStyledAttributes() which will take care of parsing the attributes for you. In particular, the Resources API will convert resource references (attribute values such as "@string/my_label" in the original XML) to the desired type for you; if you use AttributeSet directly then you will need to manually check for resource references (with getAttributeResourceValue(int, int)) and do the resource lookup yourself if needed. Direct use of AttributeSet also prevents the application of themes and styles when retrieving attribute values. This interface provides an efficient mechanism for retrieving data from compiled XML files, which can be retrieved for a particular XmlPullParser through Xml.asAttributeSet(). Normally this will return an implementation of the interface that works on top of a generic XmlPullParser, however it is more useful in conjunction with compiled XML resources:
那我们自定义View的时候,AttributeSet又是怎么用的呢?
一般情况下,我们是在values下面新建一个attrs文件夹
<declare-styleable name="MyView">
<attr name="textColor" format="color"/>
<attr name="textSize" format="dimension"/>
</declare-styleable>
布局文件如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res/com.example.androidtest"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="@android:color/black"
android:layout_height="match_parent"> <com.example.androidtest.MyView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
myapp:textColor="#FFFFFFFF"
myapp:textSize="62dp"
></com.example.androidtest.MyView> </LinearLayout>
自定义View样例代码:
public class MyView extends TextView {
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyView);
int textColor = array.getColor(R.styleable.MyView_textColor, 0XFF00FF00);
float textSize = array.getDimension(R.styleable.MyView_textSize, );
setTextColor(textColor);
setTextSize(textSize);
setText("");
array.recycle();
}
public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
Android-AttributeSet详解的更多相关文章
- Android Notification 详解(一)——基本操作
Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...
- Android Notification 详解——基本操作
Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...
- Android ActionBar详解
Android ActionBar详解 分类: Android2014-04-30 15:23 1094人阅读 评论(0) 收藏 举报 androidActionBar 目录(?)[+] 第4 ...
- Android 签名详解
Android 签名详解 AndroidOPhoneAnt设计模式Eclipse 在Android 系统中,所有安装 到 系统的应用程序都必有一个数字证书,此数字证书用于标识应用程序的作者和在应用程 ...
- Android编译系统详解(一)
++++++++++++++++++++++++++++++++++++++++++ 本文系本站原创,欢迎转载! 转载请注明出处: http://blog.csdn.net/mr_raptor/art ...
- Android布局详解之一:FrameLayout
原创文章,如有转载,请注明出处:http://blog.csdn.net/yihui823/article/details/6702273 FrameLayout是最简单的布局了.所有放在布局里的 ...
- 【整理修订】Android.mk详解
Android.mk详解 1. Android.mk 的应用范围 Android.mk文件是GNU Makefile的一小部分,它用来对Android程序进行编译. 一个Android.mk文件可以编 ...
- Android菜单详解(四)——使用上下文菜单ContextMenu
之前在<Android菜单详解(二)——创建并响应选项菜单>和<Android菜单详解(三)——SubMenu和IconMenu>中详细讲解了选项菜单,子菜单和图标菜单.今天接 ...
- Android签名详解(debug和release)
Android签名详解(debug和release) 1. 为什么要签名 1) 发送者的身份认证 由于开发商可能通过使用相同的Package Name来混淆替换已经安装的程序,以此保证签名不同的包 ...
- Android菜单详解(一)——理解android中的Menu
前言 今天看了pro android 3中menu这一章,对Android的整个menu体系有了进一步的了解,故整理下笔记与大家分享. PS:强烈推荐<Pro Android 3>,是我至 ...
随机推荐
- Google Map JavaScript API V3 实例大全
Google Map JavaScript API V3 实例大全 基础知识 简单的例子 地理位置 语言 位置 坐标 简单的投影 事件 简单事件 关闭事件 多次添加事件 事件属性 控制 php禁用ui ...
- 实习笔记-2:sql 分组不一定要group by
今天在公司写代码的时候,遇到一个sql语句构建问题. 情形是这样的: 我需要获取不同小组下前N条记录. select top 10 * from dbo.Topic where GroupID in ...
- jQuery 1.7以后 jQuery2 新元素绑定事件on替代live
最近做了一个类别动态加载的功能,jQuery版本用的是2.02. 绑定事件jQuery1.7之前用的是live或者是bind.新版的jQuery新增了on方法 由于子类别是动态加载的,默认是不会有事件 ...
- Linux下Qt环境的搭建
之前一直使用Ubuntu软件中心中的Qt4开发Qt的应用程序,现在转到Linux下来做Qt5开发,但是必须从Qt官网上面下载对应的安装包,配置起来相对麻烦一些,这里介绍整个开发流程. 首先,在官网上面 ...
- Centos7 设置IPtables
entOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl stop firewalld.service #停止fire ...
- javascript基础DOM操作
DOM 中的三个字母,D(文档)可以理解为整个 Web 加载的网页文档:O(对象)可以理解为类似 window 对象之类的东西,可以调用属性和方法,这里我们说的是 document对象:M(模型)可以 ...
- Sublime Text 3 配置浏览器预览路径 localhost
原文链接:http://jingyan.baidu.com/article/15622f2419ce79fdfcbea5ea.html 按步骤设置成功!感谢大侠! 以下是原文: Sublime Tex ...
- PHP输出
样例: $a = true;$b = false;$c = 086;$d = 0x86;echo "$a hase value: ".$a; echo "<br/& ...
- CSS3的position:sticky介绍
用户的屏幕越来越大,而页面太宽的话会不宜阅读,所以绝大部分网站的主体宽度和之前相比没有太大的变化,于是浏览器中就有越来越多的空白区域,所以你可能注意到很多网站开始在滚动的时候让一部分内容保持可见,比如 ...
- struts2处理.do后缀的请求
默认情况下,struts2是无法处理以.do为后缀的请求url的(默认情况下是.action或者不填,可以参见org.apache.struts2包下的default.properties文件). 但 ...