当前位置:   article > 正文

自定义View自定义属性_自定义view 代码设置自定义属性

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


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


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




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


  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. <com.ixgsoft.space.NewMyElement
  9. my:textValue="草了1"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. />
  13. <com.ixgsoft.space.NewMyElement
  14. my:textValue="草了2"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. />
  18. </LineanLayout>


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


2.创建自定义属性


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


  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文件中。


  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. <com.ixgsoft.space.NewMyElement
  9. my:textValue="草了1"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. />
  13. <com.ixgsoft.space.NewMyElement
  14. my:textValue="草了2"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. />
  18. </LineanLayout>



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



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


回到我们的View类MyNewElement。


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


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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/759863
推荐阅读
相关标签
  

闽ICP备14008679号