自定义属性时TypedArray的使用方法
1》在项目文件res/value下面创建一个attr.xml文件,该文件中包含若干个attr集合,例如:
[html] view plain copy print ?- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <declare-styleable name="MyView">
- <attr name="myTextSize" format="dimension"/>
- <attr name="myColor" format="color"/>
- declare-styleable>
- resources>
<?xml version="1.0" encoding="utf-8"?>
其中resource是跟标签,可以在里面定义若干个declare-styleable,
format还可以指定其他的类型比如;
reference 表示引用,参考某一资源ID
string 表示字符串
color 表示颜色值
dimension 表示尺寸值
boolean 表示布尔值
integer 表示整型值
float 表示浮点值
fraction 表示百分数
enum 表示枚举值
flag 表示位运算
2》在使用到该自定义view的布局文件中键入如下的一行:
xmlns:myapp=http://schemas.android.com/apk/res/com.eyu.attrtextdemo绿色是自己定义属性的前缀名字,粉色是项目的包名,这样一来,在我们自己定义的view的属性中,就可以使用自己在attr中定义的属性啦,例如:
[html] view plain copy print ?- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:myapp="http://schemas.android.com/apk/res/com.eyu.attrtextdemo"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context=".MainActivity" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/hello_world" />
- <com.eyu.attrtextdemo.MyView
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- myapp:myTextSize="20sp"
- myapp:myColor="#324243"/>
- LinearLayout>
3》在自定义view的代码中引入自定义属性,修改构造函数
context通过调用obtainStyledAttributes方法来获取一个TypeArray,然后由该TypeArray来对属性进行设置
obtainStyledAttributes方法有三个,我们最常用的是有一个参数的obtainStyledAttributes(int[] attrs),其参数直接styleable中获得
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView);
调用结束后务必调用recycle()方法,否则这次的设定会对下次的使用造成影响
具体如下:
[java] view plain copy print ?- package com.eyu.attrtextdemo;
- import android.content.Context;
- import android.content.res.TypedArray;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.graphics.Paint.Style;
- import android.util.AttributeSet;
- import android.view.View;
- public class MyView extends View{
- public Paint paint;
- public MyView(Context context, AttributeSet attrs) {
- super(context, attrs);
- paint = new Paint();
- TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView);
- int textColor = a.getColor(R.styleable.MyView_myColor, 003344);
- float textSize = a.getDimension(R.styleable.MyView_myTextSize, 33);
- paint.setTextSize(textSize);
- paint.setColor(textColor);
- a.recycle();
- }
- public MyView(Context context) {
- super(context);
- // TODO Auto-generated constructor stub
- }
- @Override
- protected void onDraw(Canvas canvas) {
- // TODO Auto-generated method stub
- super.onDraw(canvas);
- paint.setStyle(Style.FILL);
- canvas.drawText("aaaaaaa", 10, 50, paint);
- }
- }
package com.eyu.attrtextdemo;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.Paint.Style;import android.util.AttributeSet;import android.view.View;public class MyView extends View{public Paint paint;public MyView(Context context, AttributeSet attrs) {super(context, attrs);paint = new Paint();TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView);int textColor = a.getColor(R.styleable.MyView_myColor, 003344);float textSize = a.getDimension(R.styleable.MyView_myTextSize, 33);paint.setTextSize(textSize);paint.setColor(textColor);a.recycle();}public MyView(Context context) {super(context);// TODO Auto-generated constructor stub}@Overrideprotected void onDraw(Canvas canvas) {// TODO Auto-generated method stubsuper.onDraw(canvas);paint.setStyle(Style.FILL);canvas.drawText("aaaaaaa", 10, 50, paint);}}
运行后:
更多相关文章
- 【Android】使用Gradle打包时,获取svn的版本号,删除unalign.apk文
- 安卓view设置为满屏幕
- ym——Android从零开始(1)(Android体系架构及认识)(新)
- Android应用程序目录定义及使用方法
- android中Manifest文件的语法层次
- Android(安卓)性能分析工具之TraceView
- Android之Paint属性介绍
- Android(安卓)DrawerLayout 高仿QQ5.2双向侧滑菜单
- ANE 在 Android(安卓)上的应用