Android 自定义View为MyCustomView。在MyCustomView画了一个Rect,
颜色为Green,和一个为红色的文字。
public class MyCustomView extends View{ private Paint mPaint; private Context mContext; private static final String mString = "Hello world!"; public MyCustomView(Context context) { super(context); // TODO Auto-generated constructor stub } public MyCustomView(Context context, AttributeSet attr) { super(context,attr); // TODO Auto-generated constructor stub } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); mPaint = new Paint(); mPaint.setColor(Color.GREEN); mPaint.setStyle(Style.FILL); canvas.drawRect(new Rect(10, 10, 300, 500), mPaint); mPaint.setColor(Color.RED); canvas.drawText(mString, 50, 550, mPaint); } }
将自定义的View添加到activity_main的Layout中
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello_world" /> <com.example.customview.MyCustomView android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
其中Layout中的<com.example.customview.MyCustomView />就是对该自定义控件的使用。
二、为MyCustomView添加自定义属性
在value文件夹下建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>
MyCustomView类更改如下
public class MyCustomView extends View { private Paint mPaint; private Context mContext; private static final String mString = "Hello world!"; public MyCustomView(Context context) { super(context); mPaint = new Paint(); } public MyCustomView(Context context, AttributeSet attr) { super(context, attr); mPaint = new Paint(); TypedArray array = context.obtainStyledAttributes(attr, R.styleable.MyView); int textColor = array.getColor(R.styleable.MyView_textColor, 0XFFFFFFFF); float textSize = array.getDimension(R.styleable.MyView_textSize, 30); mPaint.setColor(textColor); mPaint.setTextSize(textSize); array.recycle(); //TypedArray通常最后调用 .recycle() 方法,为了保持以后使用该属性一致性! } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); mPaint.setStyle(Style.FILL); canvas.drawRect(new Rect(10, 10, 300, 500), mPaint); mPaint.setColor(Color.RED); canvas.drawText(mString, 50, 550, mPaint); } }
Layout更改如下
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:test="http://schemas.android.com/apk/res/com.example.customviewdemo" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <com.example.customviewdemo.MyCustomView android:layout_width="fill_parent" android:layout_height="fill_parent" test:textColor="#00ff00" test:textSize="20sp" /> </RelativeLayout>
注意:1. xmlns:test=http://schemas.android.com/apk/res/com.example.customviewdemo test是自定义的,名字随便取。com.example.customviewdemo 是包名。
2. test:textColor="#00ff00" test:textSize="20sp" 这两个就是要调用的自定义属性。
效果如下图:
作者:Work Hard Work Smart
出处:http://www.cnblogs.com/linlf03/
欢迎任何形式的转载,未经作者同意,请保留此段声明!