自定义View自定义属性

在Android开发中常常需要自定义View,在自定义View后,常常需要一些特别的属性,这里一并讲解如何自定义属性。


1.自定义一个View类:MyNewElement.java


package com.ixgsoft.space;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


/**
 * @Description: 
 */
public class NewMyElement extends View {


	
	private String TAG = "NewMyElement";
	private String text;
	
	public NewMyElement(Context context) {
		super(context);
		
	}
	public NewMyElement(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(attrs);




	}
	public NewMyElement(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(attrs);
	}


	public void init(AttributeSet attrs){
		
		TypedArray t = getContext().obtainStyledAttributes(attrs,R.styleable.NewMyElement);
		
		String textValue = t.getString(R.styleable.NewMyElement_textValue);
		float textSize = t.getDimension(R.styleable.NewMyElement_textSize, 36);
		int textColor = t.getColor(R.styleable.NewMyElement_textColor, 0xff000000);
	}
	




	
	


}




经过以上编码,就成功的创建一个自己的View类了。如果要把这个View类放到layout文件中,那么应该这样写:


<?xml version="1.0" encoding="utf-8"?>
<LineanLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:my="http://schemas.android.com/apk/res/com.ixgsoft.space"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    
	<com.ixgsoft.space.NewMyElement
        my:textValue="草了1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
	<com.ixgsoft.space.NewMyElement 
      	my:textValue="草了2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />


</LineanLayout> 


在LinearLayout中,我定义了两个NewMyElement元素,注意,自定义的View类,在Layout布局文件中必须使用完全包名,我这里使用的是com.ixgsoft.space.NewMyElement。


2.创建自定义属性


在Android中要增加自定义属性,需要依靠attrs.xml文件。这里指定的自定义属性,是在layout布局文件中使用的不是以android开头的属性,例如my:textValue。
首先,我们需要在/res/values目录下新建一个名为 attrs.xml的文件。


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="NewMyElement">
        <attr name="textColor" format="color" />
        <attr name="textSize" format="dimension" />
        <attr name="textValue" format="string" />
    </declare-styleable>
</resources>


这是一个android的resource文件,其中有一个域,名为 declare-styleable(声明属性)。这个域的有一个name属性为NewMyElement,这里很重要,这个name属性其实就是这个属性的在R类中的id。在这里域内部有3个attr域,他们都拥有两个属性,一个是name,代表这个属性的名字以及在layout布局文件中的调用名。format代表着这个属性的类型。这个属性怎么看呢?这里教大家一个方法。
在layout布局文件中,使用eclipse的提示功能(Alt+/),可以看到所有属性,这个时候按方向键,能够切换焦点,同时右部的提示也在变换。每一个android默认的属性,在提示的最后都有一个以[]包含的单词,这个就是这个属性的类型。 



目前已知的属性有这些:
reference      资源类型,通常是@开头,例如@+id/xxxx,@id/xxxxx
string             字符串类型,通常是文字信息
dimension   浮点类型,通常是尺寸度量,单位有很多px,dp,sp,dip等
color             颜色类型,通常是颜色16进制代码,支持ARGB。
boolean       布尔类型,true和false
enum           枚举类型,通常是代表这个属性提供了几种值来进行选择,并且只能选择这几种中的一个
flag             与enum基本没有区别。
integer         整数类型,通常是整数


创建完attrs.xml文件,现在我们需要把这个属性用到layout文件中。


<?xml version="1.0" encoding="utf-8"?>
<LineanLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:my="http://schemas.android.com/apk/res/com.ixgsoft.space"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    
	<com.ixgsoft.space.NewMyElement
        my:textValue="草了1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
	<com.ixgsoft.space.NewMyElement 
      	my:textValue="草了2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</LineanLayout> 



以上代码中,在根元素的中增加了一个额外的命名空间,xmlns:my="
http://schemas.android.com/apk/res/com.ixgsoft.space
"
最后的com.ixgsoft.space需要更改为你自己的包名。
做好之后,就可以在元素中使用以my开头的属性了,当然这里是没有Eclipse提示的,只能自己对照着写。



3.在代码中调用自定义属性


回到我们的View类MyNewElement。


package com.ixgsoft.space;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


/**
 * @Description: 
 */
public class NewMyElement extends View {


	
	private String TAG = "NewMyElement";
	private String text;
	
	public NewMyElement(Context context) {
		super(context);
		
	}
	public NewMyElement(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(attrs);




	}
	public NewMyElement(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(attrs);
	}


	public void init(AttributeSet attrs){
		
		TypedArray t = getContext().obtainStyledAttributes(attrs,R.styleable.NewMyElement);
		
		String textValue = t.getString(R.styleable.NewMyElement_textValue);
		float textSize = t.getDimension(R.styleable.NewMyElement_textSize, 36);
		int textColor = t.getColor(R.styleable.NewMyElement_textColor, 0xff000000);
	}
	


}


在init方法中,接收了一个AttributeSet 对象,然后使用getContext方法得到当前Context,调用Context.obtainStyledAttributes方法,传入AttributeSet 和R.styleable.NewMyElement,这里的R.styleable.NewMyElement,就是我们在attrs.xml中定义的名称,通过R.styleable来访问。
方法返回一个 typedArray对象。按照attrs,xml中定义的属性的类型,使用不同的get方法获取指定属性的值。看一看就懂了。

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值