android attributeset,Android-AttributeSet详解

android attributeset,Android-AttributeSet详解publicinterf Returnsthenu returnAposit or0iftheseti publicintget Returnsthena

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, );

setTextColor(textColor);

setTextSize(textSize);

setText(“”);

array.recycle();

}

public MyView(Context context) {

super(context);

// TODO Auto-generated constructor stub

}

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/ FrameLayout是最简单的布局了.所有放在布局里的 …

【整理修订】Android.mk详解

Android.mk详解 1. Android.mk 的应用范围 Android.mk文件是GNU Makefile的一小部分,它用来对Android程序进行编译. 一个Android.mk文件可以编 …

Android菜单详解(四)——使用上下文菜单ContextMenu

之前在和中详细讲解了选项菜单,子菜单和图标菜单.今天接 …

Android签名详解(debug和release)

Android签名详解(debug和release)   1. 为什么要签名 1) 发送者的身份认证 由于开发商可能通过使用相同的Package Name来混淆替换已经安装的程序,以此保证签名不同的包 …

Android菜单详解(一)——理解android中的Menu

前言 今天看了pro android 3中menu这一章,对Android的整个menu体系有了进一步的了解,故整理下笔记与大家分享. PS:强烈推荐,是我至 …

随机推荐

Python的高级特性12:类的继承

在面向对象的程序设计中,继承(Inheritance)允许子类从父类那里获得属性和方法,同时子类可以添加或者重载其父类中的任何方法.在C++和Java的对象模型中,子类的构造函数会自动调用父类的构造函 …

Java Web services: WS-Security with Metro–referenc

As you know from “Introducing Metro,” the reference implementations of the JAXB 2.x data-b …

J2EE 全面简介

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/198565.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月26日 下午2:55
下一篇 2026年3月26日 下午2:55


相关推荐

  • 申请CSDN博客专家的成功历程

    申请CSDN博客专家的成功历程简单说一下申请博客专家的时间、某时间段的具体数据与历程心得。(过于粗糙请见谅)本人申请博客专家的次数一共为8次,换句话说就是申请第八次才成功的。申请博客专家的时间为以下:2020.6.92020.6.152020.6.182020.7.32020.7.312020.8.212020.9.102020.9.18第八次申请终于成功了,贼开心也很激动。感谢平台的认可,我会继续努力,把最好的分享给大家。送给大家一句话、同样也是送给的自己的一句话:“一定别放弃,因为很多事情做着做着就成功了

    2022年7月15日
    17
  • Oracle-Oracle数据库备份与恢复

    Oracle-Oracle数据库备份与恢复Oracle数据库备份与恢复下面通过一些简单的例子来了解一下:Oracle数据库各种物理备份的方法。Oracle数据库各种物理恢复的方法利用RMAN工具进行数据库的备份与恢复。数据的导出与导入操作。(1)关闭BOOKSALES数据库,进行一次完全冷备份。selectfile_namefromdba_data_files;selectmemberfromv…

    2022年5月14日
    44
  • Qt-QCustomplot画静态、动态曲线教程图解

    Qt-QCustomplot画静态、动态曲线教程图解1、QCustomPlot介绍QCustomPlot是一个小型的Qt画图标类,支持绘制静态曲线、动态曲线、多重坐标曲线,柱状图,蜡烛图等。只需要在项目中加入头文件qcustomplot.h和qcustomplot.cpp文件,然后使一个widget提升为QCustomPlot类,即可使用。QCustomPlot官网:http://www.qcustomplot.com/…

    2022年10月17日
    5
  • C# OpenFileDialog SaveFileDialog Filter

    C# OpenFileDialog SaveFileDialog Filter那个Filter的格式每次都要忘,很讨厌,记录之: OpenFileDialogofd=newOpenFileDialog();ofd.Filter=”pc信息文件(*.vcf)|*.vcf|所有文件(*.*)|*.*”;if(ofd.ShowDialog()!=System.Windows.Forms.Di

    2022年7月15日
    22
  • 18.网页尺寸scrollHeight

    18.网页尺寸scrollHeight

    2021年9月4日
    53
  • c++学习笔记4,调用派生类的顺序构造和析构函数(一个)

    c++学习笔记4,调用派生类的顺序构造和析构函数(一个)

    2022年1月1日
    44

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号