Android中自定义属性(attrs.xml,TypedArray)的使用
来源:互联网 发布:南京炉石网络 编辑:程序博客网 时间:2024/05/21 11:35
该实例是在自定义View上使用自定义属性的。
MyView.java
package com.adnroid.test; import com.adnroid.test.R; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.Paint.Style; import android.util.AttributeSet; import android.view.View; public class MyView extends View { private Paint myPaint; private static final String myString = "Welcome to our Zoon!"; public MyView(Context context) { super(context); // TODO Auto-generated constructor stub } public MyView(Context context, AttributeSet attr) { super(context, attr); myPaint = new Paint(); TypedArray a = context.obtainStyledAttributes(attr, R.styleable.myView);//TypedArray是一个数组容器 float textSize = a.getDimension(R.styleable.myView_textSize, 30);//防止在XML文件里没有定义,就加上了默认值30 int textColor = a.getColor(R.styleable.myView_textColor, 0xFFFFFFFF);//同上,这里的属性是:名字_属性名 myPaint.setTextSize(textSize); myPaint.setColor(textColor); a.recycle();//我的理解是:返回以前取回的属性,供以后使用。以前取回的可能就是textSize和textColor初始化的那段 } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); //myPaint = new Paint(); myPaint.setColor(Color.RED); myPaint.setStyle(Style.FILL); canvas.drawRect(new Rect(10,10,100,100), myPaint); myPaint.setColor(Color.WHITE); canvas.drawText(myString, 10, 100, myPaint); } }
attrs.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>
main.xml
<?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.adnroid.test" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <com.adnroid.test.MyView android:layout_width="fill_parent" android:layout_height="fill_parent" test:textSize="10px" test:textColor="#fff" /> </LinearLayout>
0 0
- Android中自定义属性(attrs.xml,TypedArray)的使用
- Android中自定义属性(attrs.xml,TypedArray的使用)
- Android中自定义属性(attrs.xml,TypedArray的使用)
- Android中自定义属性(attrs.xml,TypedArray)的使用
- Android中自定义属性(attrs.xml,TypedArray的使用)
- Android中自定义属性(attrs.xml,TypedArray)的使用
- Android中自定义属性(attrs.xml,TypedArray)的使用
- Android中自定义属性(attrs.xml,TypedArray)的使用
- Android中自定义属性(attrs.xml,TypedArray的使用)
- Android中自定义属性(attrs.xml,TypedArray的使用)
- Android中自定义属性(attrs.xml,TypedArray的使用)
- Android中自定义属性(attrs.xml,TypedArray的使用)
- Android中自定义属性attrs.xml、TypedArray的使用
- 无鸯 Android中自定义属性(attrs.xml,TypedArray)的使用,
- Android学习笔记_49_Android中自定义属性(attrs.xml,TypedArray的使用)
- Android自定义模拟器skin 自定义属性(attrs.xml,TypedArray)的使用
- Android自定义View中的自定义属性(attrs.xml,TypedArray的使用)
- 自定义控件的属性(attrs.xml,TypedArray)的使用
- JAVA 数据操作流之DataOutputStream和DataInputStream
- Robotium中文API(09)-solo.clickLongOnView
- 2015.1.29图书系统调试报错
- JPA总结——实体关系映射(一对多@OneToMany)
- aspnet_regiis.exe -i
- Android中自定义属性(attrs.xml,TypedArray)的使用
- 联系人-选项-账户-进入已添加的账户,勾选联系人,点击选项立即同步,邮件项未勾选也会显示同步
- url中文编码
- Robotium中文API(10)-solo.clickOnActionBarHomeButton
- eval函数简介与PHP一句话木马剖析
- shell判断文件,目录是否存在或者具有权限
- 一对多双向自关联的实现(用户权限)
- 获取天气
- CursorAdapter刷新机制