1.首先,在values文件夹下定义一个atts.xml的文件,描述自定义的控件的属性,在values/attrs.xml中的代码如下:
1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3 <declare-styleable name="TestView">
4 <attr name="textColor" format="color" />
5 <attr name="textSize" format="dimension"/>
6 <attr name="imgBackground" format="integer"/>
7 <attr name="textPaddionLeft" format="dimension"/>
8 <attr name="textPaddingTop" format="dimension"/>
9 </declare-styleable>
10 </resources>
2.其次,定义一个继承自View的类,如:TestView,使其实现View的方法
1 package com.vanceinfo.testview;
2
3 import android.content.Context;
4 import android.content.res.TypedArray;
5 import android.graphics.Canvas;
6 import android.graphics.Color;
7 import android.graphics.Paint;
8 import android.util.AttributeSet;
9 import android.view.View;
10
11 public class TestView extends View
12 {
13 private Paint mPaint;
14
15 private Context mContext;
16
17 private String mStr;
18
19 public TestView(Context context, AttributeSet attrs)
20 {
21 // 构造方法;根据需要实现继承自View的方法
22 super(context, attrs);
23 mContext = context;
24 initTestView();
25 // 对于我们自定义的类中,我们需要使用一个名为obtainStyledAttributes的方法来获取我们的定义
26 TypedArray params = context.obtainStyledAttributes(attrs, R.styleable.TestView);
27 // 得到自定义控件的属性值。
28 int backgroundId = params.getResourceId(R.styleable.TestView_imgBackground, 0);
29 if (backgroundId != 0)
30 {
31 setBackgroundResource(backgroundId);
32 int textColor = params.getColor(R.styleable.TestView_textColor, 0XFFFFFFFF);
33 setTextColor(textColor);
34 float textSize = params.getDimension(R.styleable.TestView_textSize, 36);
35 setTextSize(textSize);
36 float padingLeft = params.getDimension(R.styleable.TestView_textPaddionLeft, 41);
37 float padingTop = params.getDimension(R.styleable.TestView_textPaddingTop, 21);
38 setPaddings(padingLeft, padingTop);
39 }
40 }
41
42 @Override
43 protected void onDraw(Canvas canvas)
44 {
45 super.onDraw(canvas);
46 if (mStr != null)
47 {
48 canvas.drawText(mStr, 30, 60, mPaint);
49 }
50 }
51
52 private void setPaddings(float padingLeft, float padingTop)
53 {
54 setPadding((int) padingLeft, (int) padingTop, 0, 0);
55 }
56
57 private void setTextColor(int textColor)
58 {
59 mPaint.setColor(0XFFAABBCC);
60 }
61
62 void setText(String text)
63 {
64 mStr = text;
65 }
66
67 private void setTextSize(float textSize)
68 {
69 mPaint.setTextSize(textSize);
70 }
71
72 private void initTestView()
73 {
74 mPaint = new Paint();
75 mPaint.setColor(Color.WHITE);
76 }
77
78 }
3.然后,在Layout文件中应用该自定义的view,如下:
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 xmlns:app="http://schmeas.android.com/apk/res/ com.vanceinfo.testview.TestView"
4 android:orientation="vertical"
5 android:layout_width="fill_parent"
6 android:layout_height="fill_parent"
7 >
8 <com.vanceinfo.testview.TestView
9 android:id="@+id/testview"
10 android:layout_width="fill_parent"
11 android:layout_height="fill_parent"
12 android:text="@string/hello"
13 app:textColor="#FFFFFFFF"
14 app:textSize="40dip"
15 app:textPaddingLeft="40dip"
16 app:textPaddingTop="40dip"
17 app:imgBackground="@drawable/beginhelp1"
18 />
19 </LinearLayout>
4.然后就是使用了,在自己的Activity 中
1 package com.vanceinfo.testview;
2
3 import android.app.Activity;
4 import android.os.Bundle;
5
6 public class TestViewActivity extends Activity
7 {
8 /** Called when the activity is first created. */
9 @Override
10 public void onCreate(Bundle savedInstanceState)
11 {
12 super.onCreate(savedInstanceState);
13 setContentView(R.layout.main);
14
15 TestView mTestView = (TestView) findViewById(R.id.testview);
16 mTestView.setText("这是自定义的View");
17 }
18 }
5效果图: