控件有很多属性,如 android:id、android:layout_width、android:layout_height等,但是这些属性都是系统自带的属性。使用attrs.xml文件,可以自己定义属性,下面我会写些小 demo ,比较好理解
内容如下:
<?xml version="1.0" encoding="utf-8"?><declare-styleable name="MyView"> <attr name="textColor" format="color" /> <attr name="textSize" format="dimension" /> </declare-styleable>其中,
<declare-styleable name="MyView">表明样式名称为MyView,下面包含了两个自定义属性textColor和textSize,其中textColor是颜色(color)类的属性,textSize是尺寸(dimension)类的属性
xmlns:test=”http://schemas.android.com/apk/res-auto”一定要添加,添加之后才能在xml中自定义属性
格式如上,其中“xmlns:test”冒号后面是标签名,在下面使用时(只对当前文件可用)
<TextView test:属性名/>“`
新闻热点
疑难解答