Android中TypedArray的用法
2016-07-07 13:57
369 查看
本文讲解了android页面对应的布局文件使用了自定义view,而自定义View添加了declare-styleable定义的自定义属性的基本的使用步骤。
1 首先,定义自定义属性,如下:
在res/values文件夹下定义一个attrs.xml文件,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="mytextview">
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension"></attr>
</declare-styleable>
</resources>
其中resource是根标签,可以在里面定义若干个declare-styleable<declare-styleable name="MyView">中name定义了变量的名称,其中<attr>中的name属性定义了属性名称,如textColor,format属性定义了属性的格式,在这里是color。下面可以再自定义多个属性,比如<attr name="myTextSize" format="dimension"/>来说,其属性的名称为"myTextSize",format指定了该属性类型为dimension,只能表示字体的大小。reference
表示引用,参考某一资源ID,另外还有其他类型如下:
string 表示字符串
color 表示颜色值
dimension 表示尺寸值
boolean 表示布尔值
integer 表示整型值
float 表示浮点值
fraction 表示百分数
enum 表示枚举值
flag 表示位运算
2 在自定义View中构造方法中获取属性,如下:
在自定义的View中通过TypedArray a = context. obtainStyledAttributes (attrs,R.styleable.mytextview);
Int textColor =a.getColor(R.styleable.mytextview_textColor, 003344);
其中R.styleable.mytextview中mytextview是<declare-styleable>中 name的值。
通过TypeArray类提供了一系列方法,如getColor,getDimension方法获取属性值。
int textColor = ta.getColor(R.styleable.mytextview_textColor, 0XFF00FF00);
其中mytextview_textColor是是<declare_styleable>中name的值+下划线+<attr>属性的值。
3 Activity页面的布局文件中使用此自定义类节点,设置自定义属性的数值,实例代码如下:
需要在声明称将包引入工程xmlns:test ="http://schemas.android.com/tools/com.example.user.testdemo"
其中:test是自定义名称,可以随意起名,值是http://schemas.android.com/tools/+包名称;
<MyTextView>添加自定义属性需要使用test:+属性名称,如test:textSize="18sp"。
1 首先,定义自定义属性,如下:
在res/values文件夹下定义一个attrs.xml文件,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="mytextview">
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension"></attr>
</declare-styleable>
</resources>
其中resource是根标签,可以在里面定义若干个declare-styleable<declare-styleable name="MyView">中name定义了变量的名称,其中<attr>中的name属性定义了属性名称,如textColor,format属性定义了属性的格式,在这里是color。下面可以再自定义多个属性,比如<attr name="myTextSize" format="dimension"/>来说,其属性的名称为"myTextSize",format指定了该属性类型为dimension,只能表示字体的大小。reference
表示引用,参考某一资源ID,另外还有其他类型如下:
string 表示字符串
color 表示颜色值
dimension 表示尺寸值
boolean 表示布尔值
integer 表示整型值
float 表示浮点值
fraction 表示百分数
enum 表示枚举值
flag 表示位运算
2 在自定义View中构造方法中获取属性,如下:
public class MyTextView extends TextView { public MyTextView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.mytextview); int textColor = ta.getColor(R.styleable.mytextview_textColor, 0XFF00FF00); float textSize = ta.getDimension(R.styleable.mytextview_textSize, 36); setTextColor(textColor); setTextSize(textSize); ta.recycle(); } }
在自定义的View中通过TypedArray a = context. obtainStyledAttributes (attrs,R.styleable.mytextview);
Int textColor =a.getColor(R.styleable.mytextview_textColor, 003344);
其中R.styleable.mytextview中mytextview是<declare-styleable>中 name的值。
通过TypeArray类提供了一系列方法,如getColor,getDimension方法获取属性值。
int textColor = ta.getColor(R.styleable.mytextview_textColor, 0XFF00FF00);
其中mytextview_textColor是是<declare_styleable>中name的值+下划线+<attr>属性的值。
3 Activity页面的布局文件中使用此自定义类节点,设置自定义属性的数值,实例代码如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:test ="http://schemas.android.com/tools/com.example.user.testdemo" android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.user.testdemo.MyTextView <span style="color:#ff0000;"> test:textSize="18sp" test:textColor="#00FF00"</span> android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我们"/> </RelativeLayout>
需要在声明称将包引入工程xmlns:test ="http://schemas.android.com/tools/com.example.user.testdemo"
其中:test是自定义名称,可以随意起名,值是http://schemas.android.com/tools/+包名称;
<MyTextView>添加自定义属性需要使用test:+属性名称,如test:textSize="18sp"。
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories