android attributeset 工具类,android attributeset总结

android attributeset 工具类,android attributeset总结一般是当项目中遇到这样的场景需要自定义控件的 AttributeSet 属性 一个自定义控件的有些属性内容是随着外部条件而动态改变的 forexample 一个自定义的 ListView 控件 需要在底部添加一个 View 而这个 View 在不同的模块使用中传入的 View 是不同的 这时候有两种方法 一种方法就是在自定义 ListView 控件类中提供一个公开的接口给外部调用从而将 View 动态的传入进去 另外一种

一般是当项目中遇到这样的场景需要自定义控件的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(“”);

array.recycle();

}

public MyView(Context context) {

super(context);

// TODO Auto-generated constructor stub

}

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

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

(0)
上一篇 2026年3月19日 上午9:09
下一篇 2026年3月19日 上午9:09


相关推荐

  • 直方图阈值法

    直方图阈值法直方图阈值法用 MATLAB 实现直方图阈值法 clc clear close I imread e role0 003i bmp I1 rgb2gray I figure subplot 2 2 1 imshow I1 title 灰度图像 gridon 显示网格线 axison 显示坐标系 m n siz

    2026年3月26日
    2
  • 开源 MQTT 服务器

    开源 MQTT 服务器到目前为止,比较流行的开源MQTT服务器有几个:1.EclipseMosquitto使用C语言实现的MQTT服务器。Eclipse组织还还包含了大量的MQTT客户端项目:https://www.eclipse.org/paho/#2.EMQX使用Erlang语言开发的MQTT服务器,内置强大的规则引擎,支持许多其他IoT协议比如MQTT-SN、CoAP、LwM2M等。3.Mosca使用Node.JS开发的MQTT服务器,简单易用

    2022年5月8日
    51
  • 推荐一个Oracle数据库学习网站

    推荐一个Oracle数据库学习网站推荐一个我个人的 Oracle 数据库学习网站 比较系统性的整理 会持续更新的网站 网址 Oracle 基础教程 http www oraclejsq com article 010100110 htmlPL SQL 教程 http www oraclejsq com plsql 010200446 html 转载于 https www cnblogs com hf131

    2026年3月26日
    2
  • gatekeeper调研

    gatekeeper调研动机及简介如果你的组织在运行 Kubernetes 那么你可能一直在寻找控制终端用户可以在集群上做什么 以及确保集群符合公司或组织政策的方法 这些政策可能是用来满足治理和法律需求 或者执行最佳实践和组织约定 使用 Kubernetes 你如何在不牺牲开发灵活性和操作独立性的情况下确保遵从性 例如 你可以执行以下政策 所有镜像必须来自已批准的存储库 所有 pod 都必须有资源限制 所有

    2026年3月18日
    2
  • java scanner输入数组_java基础- scanner/方法/数组

    java scanner输入数组_java基础- scanner/方法/数组1.用户交互scannerNext()publicclassdemo1{publicstaticvoidmain(String[]args){//创建一个scanner对象Scannerscanner=newScanner(System.in);System.out.println(“请使用next方式进行接收:”);//判断有无输入字符if(scanner.hasNext…

    2022年6月26日
    82
  • vue之watch用法

    vue之watch用法

    2021年10月11日
    60

发表回复

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

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