本帖为转载帖,稍微作了修改。 java
第一步骤:自定义属性,在res/values中定义,文件名能够自定义 android
<?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>
第二步:自定义View实现 canvas
package com.test.customview; import android.view.View; 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; /** * 这个是自定义的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 MyView extends View { Paint mPaint; // 画笔,包含了画几何图形、文本等的样式和颜色信息 public MyView(Context context) { super(context); } // 这个构造方法是系统默认的从XML文件中加载控件是的回调方法 public MyView(Context context, AttributeSet attrs) { super(context, attrs); mPaint = new Paint(); // TypedArray是一个用来存放由context.obtainStyledAttributes得到的属性的数组 // 在使用完成后,必定要调用recycle方法 // 属性的名称是styleable中的名称+“_”+属性名称 // 获取自定义的属性数组,R.styleable.MyView TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyView); int textColor = array .getColor(R.styleable.MyView_textColor, Color.BLACK); // 提供默认值,放置未指定 float textSize = array.getDimension(R.styleable.MyView_textSize, 10); mPaint.setColor(textColor); mPaint.setTextSize(textSize); array.recycle(); // 必定要调用,不然此次的设定会对下次的使用形成影响 } public void onDraw(Canvas canvas) { super.onDraw(canvas); // Canvas中含有不少画图的接口,利用这些接口,咱们能够画出咱们想要的图形 //设置画布背景颜色 canvas.drawColor(Color.BLUE); canvas.drawText("我是被画出来的", mPaint.getTextSize() + 10, mPaint.getTextSize() + 10, mPaint); // 设置画笔的颜色 mPaint.setColor(Color.RED); // 设置填充 mPaint.setStyle(Style.FILL); canvas.drawRect(10, 10, mPaint.getTextSize() + 10, mPaint.getTextSize() + 10, mPaint); // 绘制矩形 } }
第三步:布局文件: 数组
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:my="http://schemas.android.com/apk/res/com.test.customview" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.test.customview.MyView android:layout_width="match_parent" android:layout_height="wrap_content" my:textColor="@android:color/black" my:textSize="50dp" /> </LinearLayout>
这里有两个须要注意的地方,第一个是第三行,最后面一段要指定到自定义控件所在的包;第二个是在布局控件时,要写明全路径com.test.customview.MyView app
第四步:Activity ide
package com.test.customview; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class CustomView extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_custom_view); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.custom_view, menu); return true; } }