第一种
/MainActivity/res/values/attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyCircle"> <attr name="radius" format="integer"/> <attr name="mycolor" format="reference|color"/> </declare-styleable> </resources>
/MainActivity/src/com/ch/myviews/MyCircle.java
package com.ch.myviews; import com.ch.myviewattr.R; 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.view.View; public class MyCircle extends View { private Paint paint; private int radius; public MyCircle(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub //获得自定义的属性,即获得自定义的半径 TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyCircle); radius = ta.getInt(R.styleable.MyCircle_radius, 0); } //绘制的方法 @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub paint = new Paint(); paint.setColor(Color.RED); // canvas.drawColor(Color.BLACK); canvas.drawCircle(getMeasuredWidth()/2, getMeasuredHeight()/2, radius, paint); super.onDraw(canvas); } }
/MainActivity/res/layout/activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:myattr="http://schemas.android.com/apk/res/com.ch.myviewattr" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <com.ch.myviews.MyCircle android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" myattr:radius="200" /> </RelativeLayout>
第二种
/MainActivity/res/values/attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyCircle"> <attr name="radius" format="integer"/> <attr name="mycolor" format="reference|color"/> </declare-styleable> </resources>
/MainActivity/src/com/ch/myviews/MyCircle.java
package com.ch.myviews; import com.ch.myviewattr.R; 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.view.View; public class MyCircle extends View { private Paint paint; private int radius; private final static String NAMESPACE = "aaa1407a"; public MyCircle(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub //获得自定义的属性,即获得自定义的半径 radius = attrs.getAttributeIntValue(NAMESPACE, "radius", 0); } //绘制的方法 @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub paint = new Paint(); paint.setColor(Color.RED); // canvas.drawColor(Color.BLACK); canvas.drawCircle(getMeasuredWidth()/2, getMeasuredHeight()/2, radius, paint); super.onDraw(canvas); } }
/MainActivity/res/layout/activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:myattr="aaa1407a" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <com.ch.myviews.MyCircle android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" myattr:radius="200" /> </RelativeLayout>