自定义属性
自定义View如果不使用自定义属性,则背景色、前景色、文字的大小颜色都是写死的,而自定义属性可以让这些在xml中动态设置,使之更加灵活多用
创建属性文件
创建属性文件在res/values目录下,创建attrs.xml
在attrs.xml文件中写入需要自定义的属性
属性定义示例
注意:declare-styleable 标签的name属性要写为自定义View的类名,这样在xml中写的时候才会有提示
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CircleProgress">
<attr name="back_color" format="color" />
<attr name="fore_color" format="color" />
<attr name="progress_stroke_width" format="dimension" />
<attr name="progress_text_size" format="dimension" />
<attr name="progress_text_color" format="color" />
</declare-styleable>
</resources>
因为这些属性在其他自定义View中也能用到,所以可以抽出来以便复用,例如:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="back_color" format="color" />
<attr name="fore_color" format="color" />
<attr name="progress_stroke_width" format="dimension" />
<attr name="progress_text_size" format="dimension" />
<attr name="progress_text_color" format="color" />
<declare-styleable name="CircleProgress">
<attr name="back_color" />
<attr name="fore_color" />
<attr name="progress_stroke_width" />
<attr name="progress_text_size" />
<attr name="progress_text_color" />
</declare-styleable>
</resources>
属性定义也指定多种类型,例如:
<attr name="background" format="reference|color" />
对应在xml中的使用:
android:background = "@drawable/图片ID|#00FF00"
属性类型指定
name表示在xml中要写的属性名,format表示该属性的类型,类型大致有
boolean:布尔值
color:颜色
dimesion:尺寸值
-
enum:枚举
枚举的定义:
<attr name="orientation"> <enum name="horizontal" value="0" /> <enum name="vertical" value="1" /> </attr>
枚举在xml中的使用:
android:orientation = "vertical"
-
flags:标志位
标志位的定义:
< attr name="windowSoftInputMode"> < flag name = "stateUnspecified" value = "0" /> < flag name = "stateUnchanged" value = "1" /> < flag name = "stateHidden" value = "2" /> < flag name = "stateAlwaysHidden" value = "3" /> < flag name = "stateVisible" value = "4" /> < flag name = "stateAlwaysVisible" value = "5" /> < flag name = "adjustUnspecified" value = "0x00" /> < flag name = "adjustResize" value = "0x10" /> < flag name = "adjustPan" value = "0x20" /> < flag name = "adjustNothing" value = "0x30" /> < /attr>
标志位在xml中的使用:
android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden">
float:小数
fraction:百分数
integer:整形
regerence:引用,就是资源Id
string:字符串
在xml中使用自定义属性
<com.study.customview.progress.CircleProgress
android:id="@+id/circleProgress"
android:layout_width="300dp"
android:layout_height="300dp"
app:back_color="@color/gray_light"
app:fore_color="@color/cyan_dark"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:progress_stroke_width="25dp"
app:progress_text_color="@color/orange_dark"
app:progress_text_size="70sp" />
在自定义View中获取在xml中配置的属性值
init {
val typedArray =
context.obtainStyledAttributes(attrs, R.styleable.CircleProgress)
//在xml中设置的进度条背景色,如果不设置,默认为灰色,其他属性也是如此
mBackColor = typedArray.getColor(R.styleable.CircleProgress_back_color, Color.LTGRAY)
mForeColor = typedArray.getColor(R.styleable.CircleProgress_fore_color, Color.RED)
mProgressTextColor =
typedArray.getColor(R.styleable.CircleProgress_progress_text_color, Color.LTGRAY)
mProgressTextSize =
typedArray.getDimension(R.styleable.CircleProgress_progress_text_size, 20f)
mProgressStrokeWidth =
typedArray.getDimension(R.styleable.CircleProgress_progress_stroke_width, 20f)
typedArray.recycle()
......
}