自定义View如何定义和使用自己的属性
自定义View如何定义自己的属性和使用自己的属性
在Android自定义View实现很简单,继承View,重写构造函数、onDraw,(onMeasure)等函数。
如果自定义的View需要有自定义的属性,需要在values下建立attrs.xml。在其中定义你的属性,该属性没有默认值,只是说明该View包含的一些自定义的属性。
在使用到自定义View的xml布局文件中需要加入xmlns:前缀="http://schemas.android.com/apk/res/你的自定义View所在的包路径".
在使用自定义属性的时候,使用前缀:属性名,如my:textColor="#FFFFFFF"。
主程序:
package com.example.test;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.MenuItem;import android.support.v4.app.NavUtils;public class Test extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_test, menu); return true; } }
自定义View程序:
package com.example.test;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Paint.Style;import android.util.AttributeSet;import android.view.View;/** * 这个是自定义的TextView. * 至少需要重载构造方法和onDraw方法 * 对于自定义的View如果没有自己独特的属性,可以直接在xml文件中使用就可以了 * 如果含有自己独特的属性,那么就需要在构造函数中获取属性文件attrs.xml中自定义属性的名称 * 并根据需要设定默认值,放在在xml文件中没有定义。 * 如果使用自定义属性,那么在应用xml文件中需要加上新的schemas, * 比如这里是xmlns:my="http://schemas.android.com/apk/res/demo.view.my" * 其中xmlns后的“my”是自定义的属性的前缀,res后的是我们自定义View所在的包 * @author Administrator * */public class MyText extends View {private Paint mPaint;public MyText(Context context) {// TODO Auto-generated constructor stubsuper(context);}public MyText(Context context, AttributeSet attrs) {super(context, attrs);mPaint = new Paint();/* * @param AttributeSet代表继承的View本来的属性 * @param int[] 表示自定义的属性。 * @return TypedArray 包含返回属性的集合和自定义的列表 */TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyText);//textColor的值可以在Layout的XML中设置,但是在Layout的XML中的schemas必须是为//xmlns:前缀="http://schemas.android.com/apk/res/你的自定义View所在的包路径",这样就可以设置属性值。//getColor的第一个参数为XML中的属性,第二个为默认值int textColor = array.getColor(R.styleable.MyText_textColor, 0xff000000);float textSize = array.getDimension(R.styleable.MyText_textSize, 30);mPaint.setColor(textColor);mPaint.setTextSize(textSize);array.recycle();}public void draw(Canvas canvas){super.draw(canvas);mPaint.setStyle(Style.FILL);canvas.drawRect(10, 10, 100, 100, mPaint);mPaint.setColor(Color.BLUE);canvas.drawText("画出来的文字", 10, 120, mPaint);}}
自定义View的属性列表attrs.xml文件
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="MyText"> <attr name="textColor" format="color"/> <attr name="textSize" format="dimension"/> </declare-styleable> </resources>
布局文件layout_test.xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:my="http://schemas.android.com/apk/res/com.example.test" ><com.example.test.MyText android:layout_width="match_parent" android:layout_height="match_parent" my:textSize="25dp" my:textColor="#a0a0a0a0" /> </LinearLayout>
在Layout布局中设置的textColor和在View中设置的默认值不同,可以试验改变一下值。应该可以明白不少。