一般是当项目中遇到这样的场景需要自定义控件的AttributeSet属性:一个自定义控件的有些属性内容是随着外部条件而动态改变的,for example:一个自定义的ListView控件,需要在底部添加一个View,而这个View在不同的模块使用中传入的View是不同的,这时候有两种方法,一种方法就是在自定义ListView控件类中提供一个公开的接口给外部调用从而将View动态的传入进去;另外一种方法就是在通过自定义控件属性,直接类似于系统属性如Android:textsize 的用法 app:boottomView; 通过第二种方法自定义控件在XML中使用时和系统控件的属性使用方法一样,很简单、方便,而且动态、灵活、更具模块框架化,其属性内容直接在xml中动态配置,不了解其原理的人也能将该控件整合到自己的项目中快速使用起来。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文件夹

布局文件如下:

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">

android:layout_width="fill_parent"

android:layout_height="wrap_content"

myapp:textColor="#FFFFFFFF"

myapp:textSize="62dp"

>

自定义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, 36);

setTextColor(textColor);

setTextSize(textSize);

setText("22222222222");

array.recycle();

}

public MyView(Context context) {

super(context);

// TODO Auto-generated constructor stub

}

android attributeset 工具类,android attributeset总结相关推荐

  1. android sharedpreferences 工具类,android sharedpreferences工具类

    释放双眼,带上耳机,听听看~! 今天,简单讲讲如何写一个sharedpreferences的工具类. 很简单,把一些重复的操作封装在工具类里,其他地方调用就可以.在网上搜索了比较多的资料,找到一个比较 ...

  2. android attributeset 工具类,Android使用AttributeSet自定义控件的方法

    释放双眼,带上耳机,听听看~! 所谓自定义控件(或称组件)也就是编写自己的控件类型,而非Android中提供的标准的控件,如TextView,CheckBox等等.不过自定义的控件一般也都是从标准控件 ...

  3. android attributeset 工具类,android – 如何将AttributeSet传递给自定义视图

    如何将当前的AttributeSet传递给自定义View类?如果我使用的参数中只有Context的构造函数,我将丢失所有主题,并在该自定义视图的xml中使用"style"标签的能力 ...

  4. android 开发工具类,Android中常用开发工具类—持续更新...

    一.自定义ActionBar public class ActionBarTool { public static void setActionBarLayout(Activity act,Conte ...

  5. Android加密工具类,Android AES加密工具类分享

    1.AES加密工具类 java不支持PKCS7Padding,只支持PKCS5Padding.我们知道加密算法由算法+模式+填充组成,下一篇介绍iOS和Android通用的AES加密,本篇文章使用PK ...

  6. android dp工具类,Android 单位px、dp、sp转换工具类

    import android.content.Context; import android.util.TypedValue; //常用单位转换的辅助类 public DensityUtils { p ...

  7. android 计时器工具类,Android中通用定时器--好用的工具

    package com.utility.common; import android.os.Handler; import android.os.Message; public class BaseT ...

  8. android sharedpreferences工具类

    今天,简单讲讲如何写一个sharedpreferences的工具类. 很简单,把一些重复的操作封装在工具类里,其他地方调用就可以.在网上搜索了比较多的资料,找到一个比较好的工具类. 参考文章:http ...

  9. 操作SD卡,获取文件目录及文件工具类Android,listView

    操作SD卡,获取文件目录及文件工具类Android 1.获取文件目录及文件工具类: DirectoryInfo  FileScan 1.1 DirectoryInfo: package com.gls ...

最新文章

  1. IE10访问apache 2.4会奇慢的解决办法
  2. 大数据算法:排位问题(2)
  3. 自己动手写web服务器一(浏览器的访问信息)
  4. php mysql 学习周期_学习PHPMYSQL到目前为止 所有的区别特点
  5. java中方法未定义_java - Java SE中的未定义方法错误 - 堆栈内存溢出
  6. python列表删除多个相同元素_Python遍历列表删除多个元素或者重复元素
  7. 用javascript+PHP随机显示图片
  8. 20200202每日一句
  9. oracle 百分位数,oracle分析函数 percent_rank, percentile_cont, percentile_disc
  10. axure 8.0 注册码
  11. PLC Outstudio 使用教程
  12. 3. RN笔记-icon图标的使用和阿里图标的使用
  13. Excel2013函数公式大全(一)
  14. MVC+knockoutjs知识点总结
  15. 180902 逆向-网鼎(4-dalao)
  16. 利用ArcGIS创建要素与表之间的关系类并发布带有关系数据表的要素服务
  17. Unit2 附加问句
  18. keil设置c语言字体大小,Keil uVision4怎么放大字体?放大字体和关键字标注颜色方法介绍...
  19. 【转】为什么程序猿996多猝屎,而企业家007却不会?
  20. c#接口是什么,为什么要用接口

热门文章

  1. 数字音频与模拟音频的区别?
  2. 某人想将手中的一张面值100元的人民币换成10元、5元、2元和1元面值的票子。要求换正好40张,且每种票子至少一张。 问:有几种换法? input: 无 Output:该数字小于100;
  3. VBS中FileSystemObject对象详解
  4. 计算机网络(第5版)笔记-第六章 应用层
  5. 手机app连接远程mysql_Android手机app 链接服务器的mysql 读取数据库
  6. 单工,半双工,全双工
  7. 伺服器软、硬件优化及评测
  8. 智慧环保点位采集处理销号系统建设方案
  9. python - 文本文件操作的方法
  10. 启新云库值不值得入手?谈谈我的观点