自定义属性的使用方法
2015-07-21 09:45
134 查看
这段时间因为接触到了自定义的控件,无意间看到了自定义的属性的用法,但是在网上搜到的使用方法是这种的:
在res中定义一个合法命名的xml文件,在其中写入自己想要自定义的属性,注意节点名依次为<resources>和<declare-styleable name=yourname>;
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ToolBar">
<attr name="buttonNum" format="integer"/>
<attr name="itemBackground" format="reference|color"/>
</declare-styleable>
</resources>
然后在需要使用的地方定义xmlns,
如xmlns:toolbar="http://schemas.android.com/apk/res/cn.zzm.toolbar"
这个cn.zzm.toolbar是最上层的包名,一般情况下为AndroidManifest.xml中的package名字;
当然,我也见到过另外一种写法,这种写法是表示自动导入的app为命名空间的
xmlns:app="http://schemas.android.com/apk/res-auto"
这个对应的属性文件内容为:
<resources>
<declare-styleable name="RotateTextView">
<attr name="degree" format="dimension" />
</declare-styleable>
</resources>
对应的layout文件为:
<com.idunnololz.widgets.RotateTextView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/status"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:text="热热热"
android:textColor="#000000" />
这种写法也挺方便的。
在res中定义一个合法命名的xml文件,在其中写入自己想要自定义的属性,注意节点名依次为<resources>和<declare-styleable name=yourname>;
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ToolBar">
<attr name="buttonNum" format="integer"/>
<attr name="itemBackground" format="reference|color"/>
</declare-styleable>
</resources>
然后在需要使用的地方定义xmlns,
如xmlns:toolbar="http://schemas.android.com/apk/res/cn.zzm.toolbar"
这个cn.zzm.toolbar是最上层的包名,一般情况下为AndroidManifest.xml中的package名字;
当然,我也见到过另外一种写法,这种写法是表示自动导入的app为命名空间的
xmlns:app="http://schemas.android.com/apk/res-auto"
这个对应的属性文件内容为:
<resources>
<declare-styleable name="RotateTextView">
<attr name="degree" format="dimension" />
</declare-styleable>
</resources>
对应的layout文件为:
<com.idunnololz.widgets.RotateTextView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/status"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:text="热热热"
android:textColor="#000000" />
这种写法也挺方便的。
相关文章推荐
- Cocos2d-x 游戏植入广告(百度插屏)
- check 选择框checked属性读取不到
- 转载:浅谈实施软件测试风险分析
- 通过adb push 从电脑里拷贝文件到手机里
- ios开发归档与接档
- swift 里面tableview的cell代码创建方法
- sizeof()用法汇总
- ZOJ 3469 Food Delivery -- 区间dp
- Maven打war包出错
- 快速复制电脑C盘——轻松备份系统
- 表单验证插件jquery.validate的使用方法演示
- iOS之友盟错误统计解决
- hdu 1561 树形dp
- dl-ssl.google.com
- Android Binder详解
- nodejs升级,npm升级
- 【mysql】如何做到表内存在同名字段就更改记录,不存在就新增记录(replace的详细使用)
- Pig的安装和使用方法
- 转载:JSONObject.fromObject(map)(JSON与JAVA数据的转换)
- Linux下配置一个VNC服务器