1.首先,在values文件夹下定义一个atts.xml的文件,描述自定义的控件的属性,在values/attrs.xml中的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TestView">
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension"/>
<attr name="imgBackground" format="integer"/>
<attr name="textPaddionLeft" format="dimension"/>
<attr name="textPaddingTop" format="dimension"/>
</declare-styleable>
</resources>
2.其次,定义一个继承自View的类,如:TestView,使其实现View的方法
package com.vanceinfo.testview;
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 TestView extends View
{
private Paint mPaint;
private Context mContext;
private String mStr;
public TestView(Context context, AttributeSet attrs)
{
// 构造方法;根据需要实现继承自View的方法
super(context, attrs);
mContext = context;
initTestView();
// 对于我们自定义的类中,我们需要使用一个名为obtainStyledAttributes的方法来获取我们的定义
TypedArray params = context.obtainStyledAttributes(attrs, R.styleable.TestView);
// 得到自定义控件的属性值。
int backgroundId = params.getResourceId(R.styleable.TestView_imgBackground, 0);
if (backgroundId != 0)
{
setBackgroundResource(backgroundId);
int textColor = params.getColor(R.styleable.TestView_textColor, 0XFFFFFFFF);
setTextColor(textColor);
float textSize = params.getDimension(R.styleable.TestView_textSize, 36);
setTextSize(textSize);
float padingLeft = params.getDimension(R.styleable.TestView_textPaddionLeft, 41);
float padingTop = params.getDimension(R.styleable.TestView_textPaddingTop, 21);
setPaddings(padingLeft, padingTop);
}
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
if (mStr != null)
{
canvas.drawText(mStr, 30, 60, mPaint);
}
}
private void setPaddings(float padingLeft, float padingTop)
{
setPadding((int) padingLeft, (int) padingTop, 0, 0);
}
private void setTextColor(int textColor)
{
mPaint.setColor(0XFFAABBCC);
}
void setText(String text)
{
mStr = text;
}
private void setTextSize(float textSize)
{
mPaint.setTextSize(textSize);
}
private void initTestView()
{
mPaint = new Paint();
mPaint.setColor(Color.WHITE);
}
}
3.然后,在Layout文件中应用该自定义的view,如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schmeas.android.com/apk/res/ com.vanceinfo.testview.TestView" <!--"xmlns:app="http://schmeas.android.com/apk/res/"这段是固定的,"com.vanceinfo.testview.TestView"这段是继承view的类的路径 -->
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.vanceinfo.testview.TestView
android:id="@+id/testview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/hello"
app:textColor="#FFFFFFFF"
app:textSize="40dip"
app:textPaddingLeft="40dip"
app:textPaddingTop="40dip"
app:imgBackground="@drawable/beginhelp1"
/>
</LinearLayout>
4.然后就是使用了,在自己的Activity 中
package com.vanceinfo.testview;
import android.app.Activity;
import android.os.Bundle;
public class TestViewActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TestView mTestView = (TestView) findViewById(R.id.testview);
mTestView.setText("这是自定义的View");
}
}
效果图: