1.自定义view的步骤:
a.在values/attrs.xml文件中创建自定义的属性;
b.自定义view,在view的构造方法中获取到自定义的属性;
c.重写onDraw()方法;
d.重写onMeasure()方法。
在values/attrs.xml文件中创建自定义的属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="texttitle" format="string"/>
<attr name="textcolor" format="color"/>
<attr name="textsize" format="dimension"/>
<declare-styleable name="MyTextView">
<attr name="textsize"/>
<attr name="texttitle"/>
<attr name="textcolor"/>
</declare-styleable>
</resources>
设置了一个自定义的textview,设置了字体,颜色和大小的属性。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:keke="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.best.keke.mycustondemo.MyCustomView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello World!"
keke:textcolor=