Android自定义属性:attr.xml 与 TypedArray
1.attr.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyView">
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
</declare-styleable>
</resources>
2.在构造方法中使用
public MyView(Context context,AttributeSet attrs){
super(context,attrs);
mPaint = new Paint();
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyView);
int textColor = a.getColor(R.styleable.MyView_textColor, 0XFFFFFFFF); //怎么获取:名字_ 属性 连接起来就可以
float textSize = a.getDimension(R.styleable.MyView_textSize, 36);
mPaint.setTextSize(textSize);
mPaint.setColor(textColor);
a.recycle();
}
3.在布局文件中使用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:test="http://schemas.android.com/apk/res/com.android.tutor" //自定义属性前缀 与 包名
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <com.android.tutor.MyView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
test:textSize="20px"
test:textColor="#fff"/>
</LinearLayout>
Android自定义属性:attr.xml 与 TypedArray的更多相关文章
- android开发:Android 中自定义属性(attr.xml,TypedArray)的使用
今天我们的教程是根据前面一节扩展进行的,如果你没有看,请点击 Android高手进阶教程(三)查看第三课,这样跟容易方便你的理解! 在xml 文件里定义控件的属性,我们已经习惯了android:att ...
- Android 中自定义控件和属性(attr.xml,declare-styleable,TypedArray)的方法和使用
一. 在res/values 文件下定义一个attrs.xml 文件.代码如下: <?xml version="1.0" encoding="utf-8" ...
- Android中自定义属性attr.xml的格式详解
1. reference:参考某一资源ID. (1)属性定义: <declare-styleable name = "名称"> ...
- Android-深入理解android自定义属性(AttributeSet,TypedArray)
属性 自定义属性,首先要定义出来属性,我们新建一个attrs.xml: <?xml version="1.0" encoding="utf-8"?> ...
- android.content.res.TypedArray-深入理解android自定义属性(AttributeSet,TypedArray)
属性 自定义属性,首先要定义出来属性,我们新建一个attrs.xml: <?xml version="1.0" encoding="utf-8"?> ...
- Android自定义属性时TypedArray的使用方法
有时候android传统的页面布局不足以满足我们的需求,常常需要自己定义view,通常继承View,然后重写构造方法以及onDraw等函数,再 具体实现自己定义的复杂view.我们知道在给控件赋属性时 ...
- Android 自定义View二(深入了解自定义属性attrs.xml)
1.为什么要自定义属性 要使用属性,首先这个属性应该存在,所以如果我们要使用自己的属性,必须要先把他定义出来才能使用.但我们平时在写布局文件的时候好像没有自己定义属性,但我们照样可以用很多属性,这是为 ...
- (转)Android自定义属性时format选项( <attr format="reference" name="background" /> )
Android自定义属性时format选项可以取用的值 1. reference:参考某一资源ID. (1)属性定义: [html] view plaincopyprint? <declar ...
- Android自定义属性
上一篇讲解了Android自定义View,这篇来讲解一下Android自定义属性的使用,让你get新技能.希望我的分享能帮助到大家. 做Android布局是件很享受的事,这得益于他良好的xml方式.使 ...
随机推荐
- 5.3日,7:20开始 阮一峰js的早课学习
Infinity - Infinity // NaN Infinity / Infinity // NaN Infinity + Infinity // Infinity Infinity * Inf ...
- Redis-ha(sentinel)搭建
服务器描述:本次搭建是用来测试,所以是在一台服务器上搭建三个redis服务(一主两从) 服务角色 端口 Redis.conf名称 sentinel配置文件名称 sentinel端口 redis日志路径 ...
- [转]C++11 标准新特性:Defaulted 和 Deleted 函数
http://www.ibm.com/developerworks/cn/aix/library/1212_lufang_c11new/
- iOS崩溃解决记录
Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contai ...
- ERROR: JDWP Transport dt_socket failed to initialize, TRANSPORT_INIT(510)
1 ERROR: transport error 202: bind failed 2 ERROR: JDWP Transport dt_socket failed to initialize, TR ...
- org.apache.jasper.JasperException: The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in ……
编程中遇到:org.apache.jasper.JasperException: The absolute uri: http://java.sun.com/jsp/jstl/core cannot ...
- flask 中xx.init_app(app)方法
bootstrap = Bootstrap() mail = Mail() moment = Moment() db = SQLAlchemy() def create_app(config_name ...
- Meter and pixel units in a box2d game - LibGDX
http://pimentoso.blogspot.com/2013/01/meter-and-pixel-units-in-box2d-game.html 需FQ ————————————————— ...
- C语言中文网
网址:http://c.biancheng.net/cpp/ 涵盖如下:
- MAC必装神器
1. SizeUp 置中窗口的设置: Relative to Screen Size 宽80% 高80% 快捷键的设置 ctr+option+cmd+, 全窗口 ctr+option+cmd+. 置中 ...