一、基本使用流程
-
自定义 CustomAttrsView 类;
-
编写values/attrs.xml,在其中编写styleable和item等标签元素;
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="CustomAttrsView"> <attr name="name" format="string|reference"></attr> <attr name="text" format="string|reference"></attr> <attr name="android:textSize"></attr> <attr name="skinColor" format="color|reference"></attr> <attr name="isMVP" format="boolean"></attr> <attr name="kobeheight" format="float|reference"></attr> <attr name="weight" format="float|reference"></attr> <attr name="radius" format="dimension|reference"></attr> <attr name="maxPoints" format="integer|reference"></attr> <attr name="freeThrowPercent" format="fraction"></attr> <attr name="team" format="enum"> <enum name="lakers" value="0"/> <enum name="Sixers" value="1"/> <enum name="nets" value="2"/> <enum name="rockets" value="3"/> </attr> <attr name="point" format="flags"> <flag name="shoot" value="0"/> <flag name="freethrow" value="1"/> <flag name="dunk" value="2"/> </attr> </declare-styleable> </resources>
-
xml中引入自定义view,并使用自定义的属性
xmlns:tj="http://schemas.android.com/apk/res-auto" <com.yellowhat.study.customview.CustomAttrsView tj:name="kobe" tj:skinColor="#000000" tj:isMVP="true" tj:kobeheight="203" tj:weight="198" tj:radius="@dimen/dp_10" tj:maxPoints="81" tj:freeThrowPercent="8.24%" tj:team="lakers" tj:text="55" android:textSize="20sp" tj:point="dunk|shoot|freethrow" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
-
在CustomAttrsView中通过TypeArray获取各个属性;
class CustomAttrsView @JvmOverloads constructor( context: Context, attributeSet: AttributeSet? = null, defStyleAttr: Int = 0 ) : AppCompatTextView(context, attributeSet, defStyleAttr) { init { val typeArray = context.obtainStyledAttributes(attributeSet, R.styleable.CustomAttrsView) val name = typeArray.getString(R.styleable.CustomAttrsView_name) val skinColor = typeArray.getColor(R.styleable.CustomAttrsView_skinColor, Color.RED) val isMvp = typeArray.getBoolean(R.styleable.CustomAttrsView_isMVP, true) val height = typeArray.getFloat(R.styleable.CustomAttrsView_kobeheight, 195.2f) val weight = typeArray.getFloat(R.styleable.CustomAttrsView_weight, 100.0f) val circleRadius = typeArray.getDimension(R.styleable.CustomAttrsView_radius, 20.0f) val maxPoints = typeArray.getInt(R.styleable.CustomAttrsView_maxPoints, 25) val freethowPercent = typeArray.getFraction(R.styleable.CustomAttrsView_freeThrowPercent, 1, 1, 50f) val team = typeArray.getInt(R.styleable.CustomAttrsView_team, 1) val point = typeArray.getInt(R.styleable.CustomAttrsView_point, 1) val text = typeArray.getString(R.styleable.CustomAttrsView_text) val textSize = typeArray.getDimension(R.styleable.CustomAttrsView_android_textSize,22f) Log.e( "tj24", "name = ${name}\n skinColor = ${skinColor}\n isMvp = ${isMvp}\n height = ${height}\n weight = ${weight}\n circleRadius = ${circleRadius}\n maxPoints = ${maxPoints}\n freethowPercent = ${freethowPercent}\n team = ${team}\n point = ${point}\n text = ${text}\n" + " textSize = ${textSize}" ) typeArray.recycle() } }
log输出如下:
08-09 14:51:14.694 27403-27403/com.yellowhat.study E/tj24: name = kobe skinColor = -16777216 isMvp = true height = 203.0 weight = 198.0 circleRadius = 10.0 maxPoints = 81 freethowPercent = 0.082399964 team = 0 point = 3 text = 55 textSize = 20.0
二、属性值的类型format
名称 | 描述 | 备注 |
---|---|---|
reference | 参考某一资源ID | |
color | 颜色值 | |
boolean | 布尔 | |
dimension | 尺寸 | |
float | 浮点 | |
integer | 整形 | |
string | 字符串 | |
fraction | 百分比 | |
enum | 枚举 | 值为int |
flag | 位或运算 | 值为int |
三、注意事项:
- styleable 的 name 建议和自定义view 类名保持一致,不然xml中代码不会自动提示!
- xml中使用自定义属性注意命名空间,如上面 text属性有tj:text 和android:text之分;
- attr name 可以直接使用系统已定义的属性,但不能有format,如上面 textSize 属性;
- enum和 flag 的属性,value只能为int。 enum只可选一个,flag可以位运算多个。
- format可混合使用,如 format="float|reference"
网友评论