一、 背景说明:
在xml 文件里定义控件的属性,我们用的最多的是 Android系统自带的属性如:
<ImageView
android:id="@+id/iv_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@null"
android:src="@drawable/ic_ms_quick_order" />
这里小吕将自我学习与分享如何自定义属性<通常在自定义控件中会被使用到>,在xml布局文件中 如:
<!-- 搜索 -->
<com.ice.view.menubutton.MyMenuButton
xmlns:ice="http://schemas.android.com/apk/res/com.ice.view.menubutton"
android:id="@+id/btn_search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
ice:menuIcon="@drawable/ic_ms_search"
ice:menuText="@string/ms_search"
ice:textSize="14sp"
ice:textColor="@color/orange"
ice:btnCode="search" />
说明:1. 上面的MyMenuButton 是我自定义的一个菜单按钮控件 <图片和文字的组合控件>。
2. xmlns:ice=http://schemas.android.com/apk/res/com.ice.view.menubutton
是我自定义的命名空间 命名空间的名字可以自己随便定义 ,这里我定义的是ice 及 xmlns:ice 。
注意:
后面的地址则有限制,
其开始必须为:"http://schemas.android.com/apk/res/",后面则是包名com.ice.view.menubutton,
此处的包名与AndroidManifest.xml中
<manifest>节点的属性package="com.ice.view.menubutton"一致,不是自定义控件Java代码所在的包。
3. menuIcon / menuText / textSize / textColor / btnCode 就是我自定义的属性。
<只针对自定义控件 MyMenuButton有效>。
二、关于该自定义控件的开发和xml布局文件的配置大致步骤如下: