这段时间因为接触到了自定义的控件,无意间看到了自定义的属性的用法,但是在网上搜到的使用方法是这种的:
在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" />这种写法也挺方便的。