android自定义控件 ( TypedArray 、declare-styleable)

本文将简单介绍自定义控件、其中将会详细介绍关于

自定义在layout中xml布局的属性如下:

 <com.edu.testview.MyView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/s"
   		app:x="100"
        app:y="120"
        ></com.edu.testview.MyView>

一、在res/values中定义文件attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="declareXY">
        <attr name="x" format="integer"></attr>
        <attr name="y" format="integer" />
    </declare-styleable>
    
</resources>
关于如何定义该xml 详见参考链接:

二、自定义View的代码

package com.edu.testview;

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.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class MyView extends View{
	private Paint mPaint=new Paint();
	int x=1,y=1;
	
	public MyView(Context context, AttributeSet attrs){
		super(context,attrs);
		//MyView(context,attrs,1)
		getSet(context, attrs);
	}

	public MyView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// MyView(context,attrs);
		// TODO Auto-generated constructor stub
		getSet(context, attrs);
		
	}
	/**
	 * 
	* @Description: 
	*@date 2012-5-30 下午4:12:20
	 */
	private void getSet(Context context, AttributeSet attrs){
		TypedArray aTypedArray=context.obtainStyledAttributes(attrs, R.styleable.declareXY);
		int x1=aTypedArray.getInt(R.styleable.declareXY_x,30);
		int y2=aTypedArray.getInt(R.styleable.declareXY_y, 30);
		x=x1;
		y=y2;
		//
		
		int sum = attrs.getAttributeCount();
		System.out.println("sum>>>>>>>>"+sum);
		String str = "";
		for (int i = 0; i < sum; i++) {
			String name = attrs.getAttributeName(i);
			String value = attrs.getAttributeValue(i);
			str += "attr_name :" + name + ": " + value + "\n";
		}
		Log.i("attribute", str);
		
		System.out.println(str+">>>>>>>>>>");
		//System.out.println(count);
		//System.out.println(attrs.getAttributeCount());
		
		aTypedArray.recycle();
		
	}

	
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		 mPaint.setColor(Color.BLUE);
		 canvas.drawRect(new Rect(x, y, 200, 200), mPaint);  
		
	}
	
	

}


三、在layout布局中调用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res/com.edu.testview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <com.edu.testview.MyView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/s"
   		app:x="100"
        app:y="120"
        ></com.edu.testview.MyView>

</LinearLayout>

请注意   xmlns:app="http://schemas.android.com/apk/res/com.edu.testview"这句话。

                    属性的前缀名                                                 包名

源码下载地址http://download.csdn.net/detail/qq282133/4340233


参考:

Android高手进阶教程(四)之----Android 中自定义属性(attr.xml,TypedArray)的使用!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值