Android之AttributeSet案例详解
作者:暗殇 发布时间:2022-03-20 12:05:12
标签: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, 36);
setTextColor(textColor);
setTextSize(textSize);
setText("22222222222");
array.recycle();
}
public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
来源:https://www.cnblogs.com/gongcb/p/3642189.html


猜你喜欢
- 由于工作要求最近在使用GridView完成图片的批量上传功能,我的例子当中包含仿微信图片上传、拍照、本地选择、相片裁剪等功能,如果有需要的朋
- 下面以launch方法为例进行分析。一.协程的创建launch方法的代码如下:// CoroutineScope的扩展方法public fu
- 何时需要削峰当上游调用下游服务速率高于下游服务接口QPS时,那么如果不对调用速率进行控制,那么会发生很多失败请求通过消息队列的削峰方法有两种
- Code CacheJVM生成的native code存放的内存空间称之为Code Cache;JIT编译、JNI等都会编译代码到nativ
- IDEA单元测试报错:Class not found:xxxx springboot报错引入了新依赖,想着在测试模块进行测试。结果报错说Cl
- Android开发中的图片存储本来就是比较耗时耗地的事情,而使用第三方的七牛云,便可以很好的解决这些后顾之忧,最近我也是在学习七牛的SDK,
- 背景在很多场景下面我们需要在集合发生变化的时候能够通过一个事件对外进行通知,默认的List<T>并没有此类功能,所以对于这一类需
- Feign使用@RequestLine遇到的坑如何在微服务项目中调用其它项目的接口试使用spring cloud feign声明式调用。/*
- 自定义View是绘制文本有三类方法// 第一类public void drawText (String text, float x, flo
- 遇到的坑这里我把做这个功能中遇到的一些问题写在前面,是为了大家能先了解有什么问题存在,遇到这些问题的时候就不慌了,这里我把应用图标和名称先统
- 1.使用IDEA新建项目2.选择创建Maven工程3.填写GroupId和ArtifactId4.填写项目名称,与上一步的ArtifactI
- 1.剖析异或运算(^) 二元 ^ 运算符是为整型和 bool 类型预定义的。对于整型,^ 将计算操作数的按位“异或”。对于 bool 操作数
- SpringBoot配置文件的替换使用spring.profiles.active在工作中,测试或上线的时候一定会遇到的问题就是修改配置。一
- 今天把Android Studio 升级到4.1版本,发现GsonFormat没有了,网上有的解决办法从https://plugins.je
- 自动登录,是为了帮助用户多次使用这个网页时,不用再次输入用户名和密码就可以登录。自动登录是指用户将用户的登录信息,人,保存到本地的文件中Co
- 本例是利用C#中的性能计数器(PerformanceCounter)监控网络的状态。并能够直观的展现出来涉及到的知识点:Performanc
- 在对接第三方支付的时候,第三方会要求参数按照ASCII码从小到大排序。如下:public static void requestPay()
- 本文实例讲述了在C#中实现多线程中调用winform窗体控件的方法,对于C#程序设计的学习有着很好的借鉴参考价值。具体方法如下:首先,由于W
- ajax简介 Ajax 即“Asynchronous Javascript An
- JDK1.7以及以前:接口(interface)在JDK7及之前的版本对接口的要求:接口定义:使用 interface 关键字 。接口中的