Android学习笔记————初识控件自定义(Custom Components)

一般来说,android系统给我们提供的控件还是足够用了,但是,如果想要功能更丰富,更个性的控件。这时就必须我们自己来”创造“自己的控件了。当然,自定义控件的方法很多,这里跟大家分享一下,我刚刚认识的,觉得挺实用的一种方法。

首先,需要在values文件夹中创建一个attrs.xml文件,在其中定义当自定义的控件在布局文件中使用的时候需要的属性。

<?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> 
这里我定义了两个属性,它们的类别分别是color和dimension。

接下来,我们需要创建一个类,这个类就是我们需要的自定义的控件。通常我们会继承一个已有的控件,如TextView,Button等,这样,我们可以继承它们的功能,同时也拥有了独特的功能。我的控件是继承了View类,比较简单。

package com.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class MyView extends View {

	private Paint mPaint;
	
	public MyView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		mPaint = new Paint();
	}
	
	public MyView(Context context,AttributeSet attrs){
		super(context);
		//TypedArray 是一个容器,里面可以装我们自己或是系统定义的属性。
		TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView);
		
		//获取我们需要的MyView中的textColor属性,这些属性使我们自己定义的,定义在values/attrs.xml文件中
		//该属性属于color类别,所以要通过getColor方法来获取。
		int textColor = a.getColor(R.styleable.MyView_textColor,  
                 0XFFFFFFFF);  
		//获取我们需要的MyView中的textSize属性。该属性类别属于dimension所以要通过getDimension方法来获取。
		//如果没有在布局文件中使用则返回36
        float textSize = a.getDimension(R.styleable.MyView_textSize, 36);
        mPaint = new Paint();
        mPaint.setTextSize(textSize);
        mPaint.setColor(textColor);
        
        
        //TypeArray对象使用完之后要条用次方法。重新获取属性,方便我们之后使用。
        a.recycle();
	}

	/**
	 * 自定义控件最重要的一个方法,在这里我们需要根据属性来“画出”我们想要的控件。
	 */
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		canvas.drawText("这是我第一个自定义控件", 100, 100, mPaint);
	}

}

代码中的注释应该还是很清楚了吧,这里我就不详细说明了。

最后,我们就可以在布局文件中使用了,当然也可以直接在代码中使用(我不喜欢这样做,有简单的方法为什么不用呢?)。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:test="http://schemas.android.com/apk/res/com.customview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >
    <com.customview.MyView
        android:id="@+id/my_cuscom_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        test:textSize="20dp"
        test:textColor="#fff"
        />
</LinearLayout>

这里需要注意一点,就是xmlns:test="http://schemas.android.com/apk/res/com.customview"有什么用?之前对于这个问题,我也是很纠结,不明白为什么老是要在xml文件中加上xmlns:android=”http://schemas.android.com/apk/res/android"  这句话(恕在下只是浅薄,这样的小问题我都不明白),后来网上查到了答案,其实这就是用android替换了后面的东西,这样在我们输入android:xx之后按 Ctrl + /   就能提示我们接下来输入了。不知道这个答案是否准确。那么我们开头提到的那句话的用法也就相同了,最后面跟的是工程的包名。当然text可以用任意的字符串来代替。

这样一个简单的控件就产生了。

最后,附上代码,这是我写的一个例子

点击打开链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值