android使用自定义属性AttributeSet (整理而来)

这里为了演示使用自定义变量,字体大小改用自定义的属性。

首先要创建变量,创建了个values/attrs.xml文件,文件名任意,但是要在values目录下:

<?xml version="1.0" encoding="utf-8"?>   
<resources>   
    <declare-styleable name="button">   
        <attr name="textSize" format="dimension" />   
    </declare-styleable>   
</resources>

根标签要是resources,定义的变量要有个名字,declare-styleable name="button">,这里定义名称为button。在这个名称里,可以有多个自定义属性。定义了个名为textSize的属性,格式是dimension,这个format指定了textSize属性的类型,只能用于定义字体大小。

在布局文件中通过自定义属性赋值:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:myapp="http://schemas.android.com/apk/res/com.easymorse.textbutton" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent" android:background="@drawable/background_color"> 
    <LinearLayout android:layout_width="fill_parent" 
        android:layout_height="10dip" /> 
    <LinearLayout android:layout_width="fill_parent" 
        android:layout_height="40dip"> 
        <com.easymorse.textbutton.TextButton 
            android:layout_width="fill_parent" android:layout_height="fill_parent" 
            android:layout_weight="1" android:text="电影" 
            android:gravity="center_vertical|center_horizontal" 
            android:background="@drawable/button" android:focusable="true" 
            android:clickable="true" myapp:textSize="20sp" />

 

这里在根标签中增加了:

xmlns:myapp=http://schemas.android.com/apk/res/com.easymorse.textbutton

声明了myapp这个名字空间,myapp是任意的名称,自己可以随便起名,后面的:

http://schemas.android.com/apk/res/

是固定的。再后面接的是应用的包名。

在下面自定义按钮中的:myapp:textSize,就是使用<attr name="textSize"这个变量了,给变量赋值。

还需要一个过程,就是在程序中获取到这个赋值:

public TextButton(final Context context, AttributeSet attrs) { 
    this(context, attrs, 0); 
    TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.button); 
    this.setTextSize(typedArray.getDimension(R.styleable.button_textSize, 15)); 
    typedArray.recycle();

 

其中,TypedArray实例是个属性的容器,context.obtainStyledAttributes()方法返回得到。AttributeSet是节点的属性集合,在本例中是<com.easymorse.textbutton.TextButton节点中的属性集合。

这句话:

typedArray.getDimension(R.styleable.button_textSize, 
                15)

将获取自定义textSize的值,如果没有,则使用默认的值,15。

最后别忘记调用:

typedArray.recycle();

作用是:

Give back a previously retrieved StyledAttributes, for later re-use.

这里的自定义属性的format,可以有很多种:

  • reference
  • string
  • color
  • dimension
  • boolean
  • integer
  • float
  • fraction
  • enum
  • flag


所谓自定义控件(或称组件)也就是编写自己的控件类型,而非Android中提供的标准的控件,如TextView,CheckBox等等.不过自定义的控件一般也都是从标准控件继承来的,或者是多种控件组合,或者是对标准控件的属性进行改变而得到的自己满意的控件.

    自定义控件可能会有很多种方法,这里只介绍我要介绍的方法.

 

    在这种方法中,大概的步骤是这样的

    1.我们的自定义控件和其他的控件一样,应该写成一个类,而这个类的属性是是有自己来决定的.

    2.我们要在res/values目录下建立一个attrs.xml的文件,并在此文件中增加对控件的属性的定义.

    3.使用AttributeSet来完成控件类的构造函数,并在构造函数中将自定义控件类中变量与attrs.xml中的属性连接起来.

    4.在自定义控件类中使用这些已经连接的属性变量.

    5.将自定义的控件类定义到布局用的xml文件中去.

    6.在界面中生成此自定义控件类对象,并加以使用.

 

    好了,按照上述的方法,我们来看看http://blog.csdn.net/Android_Tutor/archive/2010/04/20/5508615.aspx

    博客中的实例代码,按步骤加以解释:

    //---------------------------------------------------------------------------------

    1. 定义自己的控件类:--------------------------------------------代码1.

    package com.android.tutor;  
    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 mPaint;  
        private Context mContext;  
        private static final String mString = "Welcome to Mr Wei's blog";  
      
        public MyView(Context context)
        
            super(context);  
            mPaint = new Paint();  
        

 
        public MyView(Context context,AttributeSet attrs)  
        
            super(context,attrs);  
            mPaint = new Paint();  
          
            TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView);             
            int textColor = a.getColor(R.styleable.MyView_textColor,0XFFFFFFFF);  
            float textSize = a.getDimension(R.styleable.MyView_textSize, 36);  
          
            mPaint.setTextSize(textSize);  
            mPaint.setColor(textColor);  
          
            a.recycle();  
        }

   
        @Override
        protected void onDraw(Canvas canvas)

        
            // TODO Auto-generated method stub  
            super.onDraw(canvas);  
            //设置填充  
            mPaint.setStyle(Style.FILL);  
          
            //画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标  
            canvas.drawRect(new Rect(10, 10, 100, 100), mPaint);  
          
            mPaint.setColor(Color.BLUE);  
            //绘制文字  
            canvas.drawText(mString, 10, 110, mPaint);  
        }
  
   

    代码1定义了一个自定义控件,名字为MyView,是从View类继承而来,也就是说它本身就是一种View,只是在View基础上加工而成了我们自己的自定义控件MyView.在此类种黄色的两行变量是我们新的属性变量.

 

    //---------------------------------------------------------------------------------

    2. 在res/values目录下建立一个attrs.xml的文件,并在此文件中增加对控件的属性的定义--代码2:

    <?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>

    在<resources>标签下使用<declare-styleable name="MyView">标签来告诉框架它包含的属性就是自定义控件MyView中的属性.黄色的两其实就对应了代码1中黄色的变量.

 

    //---------------------------------------------------------------------------------

    3.使用AttributeSet来完成控件类的构造函数,并在构造函数中将自定义控件类中变量与attrs.xml中的属性连接起来.

    我们再看一下代码1中的蓝色代码,其中使用AttributeSet来重载构造函数.在此函数中将类中的属性变量与代码二中定义的属性联系起来.

    //---------------------------------------------------------------------------------

    4.在自定义控件类中使用这些已经连接的属性变量.

    我们看一下代码1中的黄色部分,就是对我们新定义的属性的使用.

 

    //---------------------------------------------------------------------------------

    5.将自定义的控件类定义到布局用的xml文件中去.-----代码3:

    我们再看看布局的xml文件代码:

    <?xml version="1.0" encoding="utf-8"?>  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
        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.android.tutor.MyView  android:layout_width="fill_parent"   
            android:layout_height="fill_parent" test:textSize="20px" test:textColor="#fff" />  
    </LinearLayout>
    其中红色部分在布局中引用了我们MyView控件.

 

    //---------------------------------------------------------------------------------

    6.在界面中生成此自定义控件类对象,并加以使用.--------代码4.

 

今天我们的教程是根据前面一节扩展进行的,如果你没有看,请点击 Android高手进阶教程(三) 查看第三课,这样跟容易方便你的理解!

 

xml文件里定义控件的属性,我们已经习惯了android:attrs="",那么我们能不能定义自己的属性能,比如:test:attrs=""呢?答案是肯定的.

 

好了我就不卖关子了,直接进入主题。大致以下步骤:

 

一、res/values文件下定义一个attrs.xml文件.代码如下:

 

 

  1. 一、在res/values文件下定义一个attrs.xml文件.代码如下:  
  2. <?xml version="1.0" encoding="utf-8"?>  
  3. <resources>  
  4.     <declare-styleable name="MyView">  
  5.         <attr name="textColor" format="color" />  
  6.         <attr name="textSize" format="dimension" />  
  7.     </declare-styleable>  
  8. </resources>  

 

 

二、我们在MyView.java代码修改如下,其中下面的构造方法是重点,我们获取定义的属性我们R.sytleable.MyView_textColor,获取方法中后面通常设定默认值(float textSize = a.getDimension(R.styleable.MyView_textSize, 36);)防止我们在xml文件中没有定义.从而使用默认值!

获取,MyView就是定义在<declare-styleable name="MyView"></declare-styleable>里的名字,获取里面属性用 名字_ 属性连接起来就可以.TypedArray 通常最后调用 .recycle()方法,为了保持以后使用该属性一致性!

 

  1. public MyView(Context context,AttributeSet attrs)  
  2.     {  
  3.         super(context,attrs);  
  4.         mPaint = new Paint();  
  5.           
  6.         TypedArray a = context.obtainStyledAttributes(attrs,  
  7.                 R.styleable.MyView);  
  8.           
  9.         int textColor = a.getColor(R.styleable.MyView_textColor,  
  10.                 0XFFFFFFFF);  
  11.         float textSize = a.getDimension(R.styleable.MyView_textSize, 36);  
  12.           
  13.         mPaint.setTextSize(textSize);  
  14.         mPaint.setColor(textColor);  
  15.           
  16.         a.recycle();  
  17.     }  

 

MyView.java全部代码如下:

 

  1. package com.android.tutor;  
  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.graphics.Rect;  
  8. import android.graphics.Paint.Style;  
  9. import android.util.AttributeSet;  
  10. import android.view.View;  
  11. public class MyView extends View {  
  12.     private Paint mPaint;  
  13.     private Context mContext;  
  14.     private static final String mString = "Welcome to Mr Wei's blog";  
  15.       
  16.     public MyView(Context context) {  
  17.         super(context);  
  18.         mPaint = new Paint();  
  19.     }  
  20.     public MyView(Context context,AttributeSet attrs)  
  21.     {  
  22.         super(context,attrs);  
  23.         mPaint = new Paint();  
  24.           
  25.         TypedArray a = context.obtainStyledAttributes(attrs,  
  26.                 R.styleable.MyView);  
  27.           
  28.         int textColor = a.getColor(R.styleable.MyView_textColor,  
  29.                 0XFFFFFFFF);  
  30.         float textSize = a.getDimension(R.styleable.MyView_textSize, 36);  
  31.           
  32.         mPaint.setTextSize(textSize);  
  33.         mPaint.setColor(textColor);  
  34.           
  35.         a.recycle();  
  36.     }  
  37.     @Override  
  38.     protected void onDraw(Canvas canvas) {  
  39.         // TODO Auto-generated method stub  
  40.         super.onDraw(canvas);  
  41.         //设置填充  
  42.         mPaint.setStyle(Style.FILL);  
  43.           
  44.         //画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标  
  45.         canvas.drawRect(new Rect(1010100100), mPaint);  
  46.           
  47.         mPaint.setColor(Color.BLUE);  
  48.         //绘制文字  
  49.         canvas.drawText(mString, 10110, mPaint);  
  50.     }  
  51. }  

 

三、将我们自定义的MyView加入布局main.xml文件中,平且使用自定义属性,自定义属性必须加上:

      xmlns:test="http://schemas.android.com/apk/res/com.android.tutor"蓝色是自定义属性的前缀,红色是我们包名.

main.xml全部代码如下:

 

  1. <?xml   
  2. version="1.0" encoding="utf-8"?>  
  3. <LinearLayout   
  4. xmlns:android="http://schemas.android.com/apk/res/android"  
  5.                 
  6. xmlns:test="http://schemas.android.com/apk/res/com.android.tutor"  
  7.     android:orientation="vertical"  
  8.     android:layout_width="fill_parent"  
  9.     android:layout_height="fill_parent"  
  10.     >  
  11. <TextView    
  12.     android:layout_width="fill_parent"   
  13.     android:layout_height="wrap_content"   
  14.     android:text="@string/hello"  
  15.     />  
  16. <com.android.tutor.MyView  
  17.     android:layout_width="fill_parent"   
  18.     android:layout_height="fill_parent"   
  19.     test:textSize="20px"  
  20.     test:textColor="#fff"  
  21. />  
  22. </LinearLayout>  

 

四、运行之效果如下图:

 

 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值