Android高手进阶篇2-自定义AttributeSet属性

http://blog.csdn.net/nanzhiwen666/article/details/12224489


   前段时间因为项目的需求,而需要自定义控件的AttributeSet属性,那么我们什么时候需要这种方法来自定义控件呢?好了,不卖关子啦,直接进入主题,一般是当项目中遇到这样的场景需要自定义控件的AttributeSet属性:一个自定义控件的有些属性内容是随着外部条件而动态改变的,for example:一个自定义的ListView控件,需要在底部添加一个View,而这个View在不同的模块使用中传入的View是不同的,这时候有两种方法,一种方法就是在自定义ListView控件类中提供一个公开的接口给外部调用从而将View动态的传入进去;另外一种方法就是在通过自定义控件属性,直接类似于系统属性如Android:textsize 的用法 app:boottomView; 通过第二种方法自定义控件在XML中使用时和系统控件的属性使用方法一样,很简单、方便,而且动态、灵活、更具模块框架化,其属性内容直接在xml中动态配置,不了解其原理的人也能将该控件整合到自己的项目中快速使用起来。

  自定义控件的AttributeSet属性步骤大致如下:

一、 首先要在res/values目录下建立一个attrs.xml(名字可以自己定义)的文件,并在此文件中增加对控件的属性的定义.其xml文件如下所示:

[html]  view plain  copy
 print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.    <declare-styleable name="my_text_view">  
  4.        <attr name="text_size" format="float"></attr>  
  5.        <attr name="text_color" format="color"></attr>  
  6.        <attr name="text_back_ground" format="color|reference"></attr>  
  7.   
  8.    </declare-styleable>  
  9. </resources>  

在这里,需要补充attrs属性的相关知识,即Attr属性是如何在XML中定义的,自定义属性的Value值可以有10种类型以及其类型的组合值,其具体使用方法如下:

[java]  view plain  copy
 print ?
  1. 1. reference:参考某一资源ID。  
  2.    
  3.     (1)属性定义:  
  4.    
  5.             <declare-styleable name = "名称">  
  6.    
  7.                    <attr name = "background" format = "reference" />  
  8.    
  9.             </declare-styleable>  
  10.               
  11.    
  12.     (2)属性使用:  
  13.    
  14.             <ImageView  
  15.    
  16.                      android:layout_width = "42dip"  
  17.    
  18.                      android:layout_height = "42dip"  
  19.    
  20.                      android:background = "@drawable/图片ID"  
  21.    
  22.                      />  
  23.    
  24.   
  25.   
  26.   
  27. 2. color:颜色值。  
  28.    
  29.     (1)属性定义:  
  30.    
  31.             <declare-styleable name = "名称">  
  32.    
  33.                    <attr name = "textColor" format = "color" />  
  34.    
  35.             </declare-styleable>  
  36.    
  37.     (2)属性使用:  
  38.    
  39.             <TextView  
  40.    
  41.                      android:layout_width = "42dip"  
  42.    
  43.                      android:layout_height = "42dip"  
  44.    
  45.                      android:textColor = "#00FF00"  
  46.    
  47.                      />  
  48.    
  49.   
  50.   
  51.   
  52.  3boolean:布尔值。  
  53.    
  54.     (1)属性定义:  
  55.    
  56.             <declare-styleable name = "名称">  
  57.    
  58.                 <attr name = "focusable" format = "boolean" />  
  59.    
  60.             </declare-styleable>  
  61.    
  62.     (2)属性使用:  
  63.    
  64.             <Button  
  65.    
  66.                    android:layout_width = "42dip"  
  67.    
  68.                    android:layout_height = "42dip"  
  69.    
  70.                    android:focusable = "true"  
  71.    
  72.                     />  
  73.    
  74.    
  75.    
  76. 4. dimension:尺寸值。  
  77.    
  78.      (1)属性定义:  
  79.    
  80.              <declare-styleable name = "名称">  
  81.    
  82.                    <attr name = "layout_width" format = "dimension" />  
  83.    
  84.             </declare-styleable>  
  85.    
  86.     (2)属性使用:  
  87.    
  88.             <Button  
  89.    
  90.                    android:layout_width = "42dip"  
  91.    
  92.                    android:layout_height = "42dip"  
  93.    
  94.                   />  
  95.    
  96.   
  97.   
  98.   
  99.  5float:浮点值。  
  100.    
  101.     (1)属性定义:  
  102.    
  103.             <declare-styleable name = "AlphaAnimation">  
  104.    
  105.                    <attr name = "fromAlpha" format = "float" />  
  106.    
  107.                    <attr name = "toAlpha" format = "float" />  
  108.    
  109.             </declare-styleable>  
  110.    
  111.     (2)属性使用:  
  112.    
  113.             <alpha  
  114.    
  115.                    android:fromAlpha = "1.0"  
  116.    
  117.                    android:toAlpha = "0.7"  
  118.    
  119.                    />  
  120.    
  121.    
  122.    
  123. 6. integer:整型值。  
  124.    
  125.     (1)属性定义:  
  126.    
  127.             <declare-styleable name = "AnimatedRotateDrawable">  
  128.    
  129.                    <attr name = "visible" />  
  130.    
  131.                    <attr name = "frameDuration" format="integer" />  
  132.    
  133.                    <attr name = "framesCount" format="integer" />  
  134.    
  135.                    <attr name = "pivotX" />  
  136.    
  137.                    <attr name = "pivotY" />  
  138.    
  139.                    <attr name = "drawable" />  
  140.    
  141.             </declare-styleable>  
  142.    
  143.     (2)属性使用:  
  144.    
  145.             <animated-rotate  
  146.    
  147.                    xmlns:android = "http://schemas.android.com/apk/res/android"    
  148.    
  149.                    android:drawable = "@drawable/图片ID"    
  150.    
  151.                    android:pivotX = "50%"    
  152.    
  153.                    android:pivotY = "50%"    
  154.    
  155.                    android:framesCount = "12"    
  156.    
  157.                    android:frameDuration = "100"  
  158.    
  159.                    />  
  160.    
  161.    
  162.    
  163. 7. string:字符串。  
  164.    
  165.     (1)属性定义:  
  166.    
  167.             <declare-styleable name = "MapView">  
  168.    
  169.                    <attr name = "apiKey" format = "string" />  
  170.    
  171.             </declare-styleable>  
  172.    
  173.     (2)属性使用:  
  174.    
  175.             <com.google.android.maps.MapView  
  176.    
  177.                     android:layout_width = "fill_parent"  
  178.    
  179.                     android:layout_height = "fill_parent"  
  180.    
  181.                     android:apiKey = "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"  
  182.    
  183.                     />  
  184.    
  185.   
  186.   
  187.   
  188. 8. fraction:百分数。  
  189.    
  190.      (1)属性定义:  
  191.    
  192.             <declare-styleable name="RotateDrawable">  
  193.    
  194.                    <attr name = "visible" />  
  195.    
  196.                    <attr name = "fromDegrees" format = "float" />  
  197.    
  198.                    <attr name = "toDegrees" format = "float" />  
  199.    
  200.                    <attr name = "pivotX" format = "fraction" />  
  201.    
  202.                    <attr name = "pivotY" format = "fraction" />  
  203.    
  204.                    <attr name = "drawable" />  
  205.    
  206.             </declare-styleable>  
  207.    
  208.   
  209.   
  210.   
  211.     (2)属性使用:  
  212.    
  213.             <rotate  
  214.    
  215.                  xmlns:android = "http://schemas.android.com/apk/res/android"   
  216.    
  217.                android:interpolator = "@anim/动画ID"  
  218.    
  219.                  android:fromDegrees = "0"   
  220.    
  221.                android:toDegrees = "360"  
  222.    
  223.                  android:pivotX = "200%"  
  224.    
  225.                  android:pivotY = "300%"   
  226.    
  227.                android:duration = "5000"  
  228.    
  229.                  android:repeatMode = "restart"  
  230.    
  231.                  android:repeatCount = "infinite"  
  232.    
  233.                 />  
  234.    
  235.   
  236.   
  237.   
  238. 9enum:枚举值。  
  239.    
  240.     (1)属性定义:  
  241.    
  242.             <declare-styleable name="名称">  
  243.    
  244.                    <attr name="orientation">  
  245.    
  246.                           <enum name="horizontal" value="0" />  
  247.    
  248.                           <enum name="vertical" value="1" />  
  249.    
  250.                    </attr>              
  251.    
  252.             </declare-styleable>  
  253.    
  254.     (2)属性使用:  
  255.    
  256.             <LinearLayout  
  257.    
  258.                     xmlns:android = "http://schemas.android.com/apk/res/android"  
  259.    
  260.                     android:orientation = "vertical"  
  261.    
  262.                     android:layout_width = "fill_parent"  
  263.    
  264.                     android:layout_height = "fill_parent"  
  265.    
  266.                     >  
  267.    
  268.             </LinearLayout>  
  269.    
  270.   
  271.   
  272.   
  273. 10. flag:位或运算。  
  274.    
  275.      (1)属性定义:  
  276.    
  277.              <declare-styleable name="名称">  
  278.    
  279.                     <attr name="windowSoftInputMode">  
  280.    
  281.                             <flag name = "stateUnspecified" value = "0" />  
  282.    
  283.                             <flag name = "stateUnchanged" value = "1" />  
  284.    
  285.                             <flag name = "stateHidden" value = "2" />  
  286.    
  287.                             <flag name = "stateAlwaysHidden" value = "3" />  
  288.    
  289.                             <flag name = "stateVisible" value = "4" />  
  290.    
  291.                             <flag name = "stateAlwaysVisible" value = "5" />  
  292.    
  293.                             <flag name = "adjustUnspecified" value = "0x00" />  
  294.    
  295.                             <flag name = "adjustResize" value = "0x10" />  
  296.    
  297.                             <flag name = "adjustPan" value = "0x20" />  
  298.    
  299.                             <flag name = "adjustNothing" value = "0x30" />  
  300.    
  301.                      </attr>           
  302.    
  303.              </declare-styleable>  
  304.    
  305.   
  306.   
  307.   
  308.      (2)属性使用:  
  309.    
  310.             <activity  
  311.    
  312.                    android:name = ".StyleAndThemeActivity"  
  313.    
  314.                    android:label = "@string/app_name"  
  315.    
  316.                    android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden">  
  317.    
  318.                    <intent-filter>  
  319.    
  320.                           <action android:name = "android.intent.action.MAIN" />  
  321.    
  322.                           <category android:name = "android.intent.category.LAUNCHER" />  
  323.    
  324.                    </intent-filter>  
  325.    
  326.              </activity>  
  327.    
  328.      注意:  
  329.    
  330.      属性定义时可以指定多种类型值。  
  331.    
  332.     (1)属性定义:  
  333.    
  334.             <declare-styleable name = "名称">  
  335.    
  336.                    <attr name = "background" format = "reference|color" />  
  337.    
  338.             </declare-styleable>  
  339.    
  340.     (2)属性使用:  
  341.    
  342.              <ImageView  
  343.    
  344.                      android:layout_width = "42dip"  
  345.    
  346.                      android:layout_height = "42dip"  
  347.    
  348.                      android:background = "@drawable/图片ID|#00FF00"  
  349.    
  350.                      />  


二、接下来实现自定义View的类,其中下面的构造方法是重点,在代码中获取自定义属性,其代码如下:

[java]  view plain  copy
 print ?
  1. package com.example.CustomAttr;  
  2.   
  3. import android.content.Context;  
  4. import android.content.res.TypedArray;  
  5. import android.graphics.Paint;  
  6. import android.util.AttributeSet;  
  7. import android.util.TypedValue;  
  8. import android.widget.TextView;  
  9.   
  10. /** 
  11.  * Created with IntelliJ IDEA. 
  12.  * User: zhiwen.nan 
  13.  * Date: 13-9-28 
  14.  * Time: 下午10:00 
  15.  * To change this template use File | Settings | File Templates. 
  16.  */  
  17. public class CustomTextView extends TextView {  
  18.     private TypedArray mTypedArray;  
  19.     private Paint mPaint;  
  20.   
  21.     public CustomTextView(Context context) {  
  22.         super(context);  
  23.     }  
  24.   
  25.     public CustomTextView(Context context, AttributeSet attrs) {  
  26.         super(context, attrs);  
  27.         inial(context,attrs);  
  28.     }  
  29.   
  30.     public CustomTextView(Context context, AttributeSet attrs, int defStyle) {  
  31.         super(context, attrs, defStyle);  
  32.         inial(context,attrs);  
  33.     }  
  34.   
  35.     private void inial(Context context,AttributeSet attrs)  {  
  36.         TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.my_text_view);  
  37.   
  38.         float textsize = typedArray.getFloat(R.styleable.my_text_view_text_size,14)   ;  
  39.         int textColor = typedArray.getColor(R.styleable.my_text_view_text_color,0xFFFFFF)   ;  
  40.         int bgColor = typedArray.getColor(R.styleable.my_text_view_text_back_ground,0xFFFFFF) ;  
  41.   
  42.         super.setTextColor(textColor);  
  43.         super.setTextSize(textsize);  
  44.         super.setBackgroundColor(bgColor);  
  45.   
  46.   
  47.         typedArray.recycle();  
  48.   
  49.     }  
  50.   
  51.   
  52.   
  53. }  


三、接下来在XML布局中引用自定义View控件,其XML代码如下:

[html]  view plain  copy
 print ?
  1. ?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.                 xmlns:app = "http://schemas.android.com/apk/res/com.example.CustomAttr"  
  4.               android:orientation="vertical"  
  5.               android:layout_width="fill_parent"  
  6.               android:layout_height="fill_parent"  
  7.         >  
  8.   
  9.     <com.example.CustomAttr.CustomTextView  
  10.             android:layout_width="fill_parent"  
  11.             android:layout_height="wrap_content"  
  12.             android:text="Hello World, MainActivity"  
  13.             app:text_size ="20"  
  14.             app:text_color ="#00FF00"  
  15.             app:text_back_ground="#ffffff"  
  16.   
  17.             />  
  18. </LinearLayout>  


注意上面XML中代码:   xmlns:app = "http://schemas.android.com/apk/res/com.example.CustomAttr",是自定义的app命名空间,res后面是应用程序包名,然后可以直接使用app:text_size,等属性,其值类型要和attrs.xml定义的属性Value值相对应。

四、总结:

    注意该例子中是使用app:text_size = "20 和app:text_color="#00FF00定义TextView的颜色和textView的字体大小,而不是使用系统的属性android:textsize等。该例子中只是起到抛砖引玉的作用,你可以自定义其他属性,来实现你想要的自定义View效果。

例子资源下载:  http://download.csdn.net/detail/nanzhiwen666/6346979


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值