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

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

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

   
   
       
           
   
   

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

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

 
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"> 
            android:layout_height="10dip" /> 
            android:layout_height="40dip"> 
                    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,就是使用

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

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是节点的属性集合,在本例中是

这句话:

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);  
          
           

你可能感兴趣的:(android_draw)