自定义View自定义属性

转自:https://i-blog.csdnimg.cn/blog_migrate/370a96dbaaabca17e93a2f916f087169.png




在Android开发中常常需要自定义View,在自定义View后,常常需要一些特别的属性,这里一并讲解如何自定义属性。


1.自定义一个View类:MyNewElement.java


[java]  view plain copy
  1. package com.ixgsoft.space;  
  2.   
  3.   
  4. import android.content.Context;  
  5. import android.content.res.TypedArray;  
  6. import android.graphics.Canvas;  
  7. import android.graphics.Color;  
  8. import android.graphics.Paint;  
  9. import android.util.AttributeSet;  
  10. import android.util.Log;  
  11. import android.view.MotionEvent;  
  12. import android.view.View;  
  13. import android.widget.Button;  
  14. import android.widget.TextView;  
  15. import android.widget.Toast;  
  16.   
  17.   
  18. /** 
  19.  * @Description:  
  20.  */  
  21. public class NewMyElement extends View {  
  22.   
  23.   
  24.       
  25.     private String TAG = "NewMyElement";  
  26.     private String text;  
  27.       
  28.     public NewMyElement(Context context) {  
  29.         super(context);  
  30.           
  31.     }  
  32.     public NewMyElement(Context context, AttributeSet attrs) {  
  33.         super(context, attrs);  
  34.         init(attrs);  
  35.   
  36.   
  37.   
  38.   
  39.     }  
  40.     public NewMyElement(Context context, AttributeSet attrs, int defStyle) {  
  41.         super(context, attrs, defStyle);  
  42.         init(attrs);  
  43.     }  
  44.   
  45.   
  46.     public void init(AttributeSet attrs){  
  47.           
  48.         TypedArray t = getContext().obtainStyledAttributes(attrs,R.styleable.NewMyElement);  
  49.           
  50.         String textValue = t.getString(R.styleable.NewMyElement_textValue);  
  51.         float textSize = t.getDimension(R.styleable.NewMyElement_textSize, 36);  
  52.         int textColor = t.getColor(R.styleable.NewMyElement_textColor, 0xff000000);  
  53.     }  
  54.       
  55.   
  56.   
  57.   
  58.   
  59.       
  60.       
  61.   
  62.   
  63. }  




经过以上编码,就成功的创建一个自己的View类了。如果要把这个View类放到layout文件中,那么应该这样写:


[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LineanLayout   
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:my="http://schemas.android.com/apk/res/com.ixgsoft.space"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     >  
  8.       
  9.     <com.ixgsoft.space.NewMyElement  
  10.         my:textValue="草了1"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         />  
  14.     <com.ixgsoft.space.NewMyElement   
  15.         my:textValue="草了2"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         />  
  19.   
  20.   
  21. </LineanLayout>   


在LinearLayout中,我定义了两个NewMyElement元素,注意,自定义的View类,在Layout布局文件中必须使用完全包名,我这里使用的是com.ixgsoft.space.NewMyElement。


2.创建自定义属性


在Android中要增加自定义属性,需要依靠attrs.xml文件。这里指定的自定义属性,是在layout布局文件中使用的不是以android开头的属性,例如my:textValue。
首先,我们需要在/res/values目录下新建一个名为 attrs.xml的文件。


[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <declare-styleable name="NewMyElement">  
  4.         <attr name="textColor" format="color" />  
  5.         <attr name="textSize" format="dimension" />  
  6.         <attr name="textValue" format="string" />  
  7.     </declare-styleable>  
  8. </resources>  


这是一个android的resource文件,其中有一个域,名为 declare-styleable(声明属性)。这个域的有一个name属性为NewMyElement,这里很重要,这个name属性其实就是这个属性的在R类中的id。在这里域内部有3个attr域,他们都拥有两个属性,一个是name,代表这个属性的名字以及在layout布局文件中的调用名。format代表着这个属性的类型。这个属性怎么看呢?这里教大家一个方法。
在layout布局文件中,使用eclipse的提示功能(Alt+/),可以看到所有属性,这个时候按方向键,能够切换焦点,同时右部的提示也在变换。每一个android默认的属性,在提示的最后都有一个以[]包含的单词,这个就是这个属性的类型。 



目前已知的属性有这些:
reference      资源类型,通常是@开头,例如@+id/xxxx,@id/xxxxx
string             字符串类型,通常是文字信息
dimension   浮点类型,通常是尺寸度量,单位有很多px,dp,sp,dip等
color             颜色类型,通常是颜色16进制代码,支持ARGB。
boolean       布尔类型,true和false
enum           枚举类型,通常是代表这个属性提供了几种值来进行选择,并且只能选择这几种中的一个
flag             与enum基本没有区别。
integer         整数类型,通常是整数


创建完attrs.xml文件,现在我们需要把这个属性用到layout文件中。


[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LineanLayout   
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:my="http://schemas.android.com/apk/res/com.ixgsoft.space"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     >  
  8.       
  9.     <com.ixgsoft.space.NewMyElement  
  10.         my:textValue="草了1"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         />  
  14.     <com.ixgsoft.space.NewMyElement   
  15.         my:textValue="草了2"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         />  
  19.   
  20. </LineanLayout>   



以上代码中,在根元素的中增加了一个额外的命名空间,xmlns:my="
http://schemas.android.com/apk/res/com.ixgsoft.space
"
最后的com.ixgsoft.space需要更改为你自己的包名。
做好之后,就可以在元素中使用以my开头的属性了,当然这里是没有Eclipse提示的,只能自己对照着写。



3.在代码中调用自定义属性


回到我们的View类MyNewElement。


[java]  view plain copy
  1. package com.ixgsoft.space;  
  2.   
  3.   
  4. import android.content.Context;  
  5. import android.content.res.TypedArray;  
  6. import android.graphics.Canvas;  
  7. import android.graphics.Color;  
  8. import android.graphics.Paint;  
  9. import android.util.AttributeSet;  
  10. import android.util.Log;  
  11. import android.view.MotionEvent;  
  12. import android.view.View;  
  13. import android.widget.Button;  
  14. import android.widget.TextView;  
  15. import android.widget.Toast;  
  16.   
  17.   
  18. /** 
  19.  * @Description:  
  20.  */  
  21. public class NewMyElement extends View {  
  22.   
  23.   
  24.       
  25.     private String TAG = "NewMyElement";  
  26.     private String text;  
  27.       
  28.     public NewMyElement(Context context) {  
  29.         super(context);  
  30.           
  31.     }  
  32.     public NewMyElement(Context context, AttributeSet attrs) {  
  33.         super(context, attrs);  
  34.         init(attrs);  
  35.   
  36.   
  37.   
  38.   
  39.     }  
  40.     public NewMyElement(Context context, AttributeSet attrs, int defStyle) {  
  41.         super(context, attrs, defStyle);  
  42.         init(attrs);  
  43.     }  
  44.   
  45.   
  46.     public void init(AttributeSet attrs){  
  47.           
  48.         TypedArray t = getContext().obtainStyledAttributes(attrs,R.styleable.NewMyElement);  
  49.           
  50.         String textValue = t.getString(R.styleable.NewMyElement_textValue);  
  51.         float textSize = t.getDimension(R.styleable.NewMyElement_textSize, 36);  
  52.         int textColor = t.getColor(R.styleable.NewMyElement_textColor, 0xff000000);  
  53.     }  
  54.       
  55.   
  56.   
  57. }  


在init方法中,接收了一个AttributeSet 对象,然后使用getContext方法得到当前Context,调用Context.obtainStyledAttributes方法,传入AttributeSet 和R.styleable.NewMyElement,这里的R.styleable.NewMyElement,就是我们在attrs.xml中定义的名称,通过R.styleable来访问。
方法返回一个 typedArray对象。按照attrs,xml中定义的属性的类型,使用不同的get方法获取指定属性的值。看一看就懂了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值