一、在res/values/attrs.xml文件中:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--声明一个属性,名称可以随意,一般将名称 与拥有这些属性的类名相同 -->
<declare-styleable name="SettingView">
<attr name="title" format="string" />
<attr name="desc" format="string" />
</declare-styleable>
</resources>
二、在布局文件中使用声明的属性
1、声明命名空间:
2、给view添加自定的属性:
<com.itheima.mobilesafe45.view.SettingView
android:id="@+id/sv_autoUpdate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
itheima:title="自动更新设置"
itheima:desc="自动更新已经打开#自动更新已经关闭"
/>
三、在代码中解析,获得这些属性
// attrs.getAttributeCount();
// attrs.getAttributeName();
// attrs.getAttributeValue();
String title = attrs.getAttributeValue("
http://schemas.android.com/apk/res/com.itheima.mobilesafe45","title");
String desc = attrs.getAttributeValue("
http://schemas.android.com/apk/res/com.itheima.mobilesafe45","desc");