自定义属性

怎么给自己定义的View设置属性(让这些属性直接在xml使用)如何自定义属性

步骤:确定什么是外部配置的

1.在 values 里创建 attr 的 xml 文件

设置name和对应的类型
res → values → (new resource file) attrs

< declare-styleable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--选择给那个View定义属性-->
<declare-styleable name="UserInputView">
<!--自定义相应属性-->

<!--标题-->
<attr name="title" format="string|reference"/>
<!--默认提示内容-->
<attr name="placeholder" format="string|reference"/>

<!--错误时提示内容-->
<attr name="error_title" format="string|reference"/>

<!--图片-->
<attr name="icon" format="reference"/>!



<!--设置类型: 密码输入 or 正常输入-->
<attr name="input_type" format="integer">
<enum name="password" value="1"/>
<enum name="normal" value="2"/>
</attr>
</declare-styleable>

<declare-styleable name="ToolView">
<attr name="icon" format="reference"/>
<attr name="bg_icon" format="reference"/>
<attr name="icon_size" format="dimension"/>
<attr name="tool_type" format="enum">
<enum name="move" value="1"/>
<enum name="delete" value="2"/>
<enum name="back" value="3"/>
<enum name="eraser" value="4"/>
<enum name="curve" value="5"/>
<enum name="line" value="6"/>
<enum name="triangle" value="7"/>
<enum name="circle" value="8"/>
<enum name="rectangle" value="9"/>
<enum name="bezel" value="10"/>
<enum name="brush" value="11"/>
<enum name="none" value="12"/>
<enum name="nothing" value="13"/>
</attr>


</resources>

2.Xml中使用自定义的属性

3.在自定义的 View 中,解析 xml 中自定义的属性

并把这些属性的值设置给对应的控件 //使用

把对应的属性解析出来 init中解析对应的属性(只有在 init 中才能访问构造方法中的 attrs ) (需要提供一个参数存对应的属性)

从attrs里面解析出R.styleable.UserInputView里面自定义的对应的属性和值:
context.obtainStyledAttributes (attrs[属性],R.styleable.CustomTextView[需要解析的东西即对应的资源文件])

得到对应的属性和值:
getString()
getInteger()
getFloat()
getDrawable() Drawable图片资源

typeArray.recycle() //回收

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//解析xml中自定义的属性 -> 拆包
//①从attrs里面解析出R.styleable.UserInputView里面自定义的对应的属性和值
val typedArray = context.obtainStyledAttributes(attrs,R.styleable.UserInputView)

//②从typedArray中解析每一个属性的值 取出attrs.xml中定义的值 并使用
binding.titleTextView.text = typedArray.getString(R.styleable.UserInputView_title)?:"设置标题"
binding.inputEditText.hint = typedArray.getString(R.styleable.UserInputView_placeholder)

val inputType = typedArray.getInteger(R.styleable.UserInputView_input_type,2)
if (inputType == 1){ //设置密码不可见
binding.inputEditText.inputType = InputType.TYPE_TEXT_VARIATION_PASSWORD or InputType.TYPE_CLASS_TEXT
}

//③回收
typeArray.recycle()



//----------------------------------
//自定义的属性
private var mIcon:Drawable //图标
private var mBgIcon:Drawable //背景
private var mIconSize = 0 //大小

private var mType: ToolType
context.obtainStyledAttributes(attrs, R.styleable.ToolView).apply {
mIcon = getDrawable(R.styleable.ToolView_icon)?:resources.getDrawable(R.drawable.logo,null)
mBgIcon = getDrawable(R.styleable.ToolView_bg_icon)?:resources.getDrawable(R.drawable.round_bg,null)
mIconSize = getDimension(R.styleable.ToolView_icon_size,dp2px(16)).toInt()
val enumValue = getInteger(R.styleable.ToolView_tool_type,ToolType.None.value)
mType = ToolType.values()[enumValue-1]
recycle()
}
1
2
3
4
5
6
<!--xml中设置密码不可见-->
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

扫一扫,分享到微信

微信分享二维码
  • Copyrights © 2023-2025 Annie
  • Visitors: | Views:

嘿嘿 请我吃小蛋糕吧~

支付宝
微信