android随笔13——自定义属性

来源:互联网 发布:mac 如何设置密码 编辑:程序博客网 时间:2024/06/05 23:00

自定义属性的步骤:

 一:声明属性  

在valus中创建xml文件 attrs.xml 内容参数系统的attrs.xm

​-- 声明一个属性集合,名称可以随意,一般和拥有这些属性的类,同名 --          "<"  declare-styleable name="SettingItemView"  ">"          -- 声明一个属性名称可以随意,类型是字符串类型 --          "<" attr name="sv_title" format="string" / ">"        -- 声明一个属性名称可以随意,类型是字符串类型 --         "<" attr name="sv_descs" format="string" / ">""<"/declare-styleable">"​"<"/resources">"

二:在布局中使用属性  

1- 声明命名空间: 

xmlns:itcast="http://schemas.android.com/apk/res/zz.itcast.mobileguardz10"    <span style="color:#ff0000;">xmlns xml name space     itcast 是命名空间的缩写    将最后一个android 改为当前应用的包名</span>
<span style="color: rgb(51, 51, 51); font-family: 'Microsoft Yahei', Simsun; font-size: 24px; line-height: 38px;">2- 使用属性</span>
android:id="@+id/sv_auto_update"             android:layout_width="match_parent"              itcast:sv_title = "自动更新设置"              itcast:sv_descs = "自动更新已经开启#自动更新没有开启"   android:layout_height="wrap_content"             android:background="@drawable/grid_item_bg" />

三:在代码中获得属性的中值 




0 0
原创粉丝点击