Android自定义View及自定义控件属性declare-styleable:自定义控件的属性(attr.xml,TypedArray)的使用

以launcher为例说明自定义控件的属性:

1、  在layout里面定义控件,如:

<com.junction.launcher.DragLayer

     xmlns:android="http://schemas.android.com/apk/res/android"

     xmlns:launcher="http://schemas.android.com/apk/res/com.junction.launcher"

    android:id="@+id/drag_layer" android:layout_width="match_parent"  android:layout_height="match_parent">

  <com.android.launcher.Workspace

        android:id="@+id/workspace"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        launcher:defaultScreen="1">


        <include android:id="@+id/cell1" layout="@layout/workspace_screen" />

        <include android:id="@+id/cell2" layout="@layout/workspace_screen" />

        <include android:id="@+id/cell3" layout="@layout/workspace_screen" />

    </com.android.launcher.Workspace>

2、  该控件workspace下面有一个属性是launcher:defaultScreen,这属性不是ViewGroup(workspace类是继承于ViewGroup)所定义的属性,那么我们可以在\res\values里面的attrs.xml里面定义

具体的定义方法如下:

<resources>

<declare-styleable name="Workspace">

    <attr name="defaultScreen" format="integer"  />

    </declare-styleable>

</resources>

引用方法是 先申明xmlns:launcher="http://schemas.android.com/apk/res/com.junction.launcher"(R.java),这样就可以使用launcher:defaultScreen。

3、  这样我们在Workspace的类里面就可以去除在layout里面对该属性的赋值,方法如下:

Int mDefaultScreen;

 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Workspace, defStyle, 0);

mDefaultScreen = a.getInt(R.styleable.Workspace_defaultScreen, 1);

a.recycle();

++++++++++++++++++++++++++++++

自己案例:可看一个gifPlayer的demo程序。

其他案例:http://blog.csdn.net/Android_Tutor/archive/2010/04/20/5508615.aspx

++++++++++++++++++++++++++++++

转载自:http://blog.csdn.net/tinafhx/archive/2010/02/05/5290878.aspx



Android自定义View实现很简单

继承View,重写构造函数、onDraw,(onMeasure)等函数。

如果自定义的View需要有自定义的属性,需要在values下建立attrs.xml。在其中定义你的属性。

在使用到自定义View的xml布局文件中需要加入xmlns:前缀="http://schemas.android.com/apk/res/你的自定义View所在的包路径".

在使用自定义属性的时候,使用前缀:属性名,如my:textColor="#FFFFFFF"。

实例:

 

  1. package demo.view.my;   
  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.Paint.Style;   
  8. import android.util.AttributeSet;   
  9. import android.view.View;   
  10. /**  
  11.  * 这个是自定义的TextView.  
  12.  * 至少需要重载构造方法和onDraw方法  
  13.  * 对于自定义的View如果没有自己独特的属性,可以直接在xml文件中使用就可以了  
  14.  * 如果含有自己独特的属性,那么就需要在构造函数中获取属性文件attrs.xml中自定义属性的名称  
  15.  * 并根据需要设定默认值,放在在xml文件中没有定义。  
  16.  * 如果使用自定义属性,那么在应用xml文件中需要加上新的schemas,  
  17.  * 比如这里是xmlns:my="http://schemas.android.com/apk/res/demo.view.my" 
  18.  * 其中xmlns后的“my”是自定义的属性的前缀,res后的是我们自定义View所在的包  
  19.  * @author Administrator  
  20.  *  
  21.  */  
  22. public class MyView extends View {   
  23.        
  24.     Paint mPaint; //画笔,包含了画几何图形、文本等的样式和颜色信息  
  25.     public MyView(Context context) {   
  26.         super(context);   
  27.            
  28.     }   
  29.        
  30.     public MyView(Context context, AttributeSet attrs){   
  31.         super(context, attrs);   
  32.         mPaint = new Paint();   
  33.         //TypedArray是一个用来存放由context.obtainStyledAttributes获得的属性的数组  
  34.         //在使用完成后,一定要调用recycle方法   
  35.         //属性的名称是styleable中的名称+“_”+属性名称  
  36.         TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyView);   
  37.         int textColor = array.getColor(R.styleable.MyView_textColor, 0XFF00FF00); //提供默认值,放置未指定  
  38.         float textSize = array.getDimension(R.styleable.MyView_textSize, 36);   
  39.         mPaint.setColor(textColor);   
  40.         mPaint.setTextSize(textSize);   
  41.            
  42.         array.recycle(); //一定要调用,否则这次的设定会对下次的使用造成影响  
  43.     }   
  44.        
  45.     public void onDraw(Canvas canvas){   
  46.         super.onDraw(canvas);   
  47.         //Canvas中含有很多画图的接口,利用这些接口,我们可以画出我们想要的图形  
  48.         //mPaint = new Paint();   
  49.         //mPaint.setColor(Color.RED);  
  50.         mPaint.setStyle(Style.FILL); //设置填充  
  51.         canvas.drawRect(1010100100, mPaint); //绘制矩形  
  52.            
  53.         mPaint.setColor(Color.BLUE);   
  54.         canvas.drawText("我是被画出来的"10120, mPaint);   
  55.     }   
  56. }  

 

相应的属性文件:

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <declare-styleable name="MyView">  
  4.         <attr name="textColor" format="color"/>  
  5.         <attr name="textSize" format="dimension"/>  
  6.     </declare-styleable>  
  7. </resources>  

 

在布局文件中的使用:

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.               xmlns:my="http://schemas.android.com/apk/res/demo.view.my"    
  4.     android:orientation="vertical"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="fill_parent"  
  7.     >  
  8.        
  9.     <demo.view.my.MyView  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"    
  12.         my:textColor="#FFFFFFFF"    
  13.         my:textSize="22dp"  
  14.         />  
  15. </LinearLayout>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值