在attrs.xml文件中声明自定义View的类和相应属性及其数据类型
在自定义View中对自定义属性进行解析
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ToolBar);
buttonNum = ta.getInt(R.styleable.ToolBar_buttonNum, 5);
itemBg = ta.getResourceId(R.styleable.ToolBar_itemBackground, -1);
ta.recycle();
在布局文件中使用自定义属性,声明命名空间
xmlns:toolbar="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
toolbar:icon="@drawble/icon"
toolbar:labelColor="#29C1B2"
toolbar:isVisible="true"
toolbar:width="180dp"
toolbar:fromAlpha="0.5"
toolbar:buttonNum="3"
toolbar:label="ToolBar"
toolbar:pivotX="30%"
toolbar:language="english"
toolbar:windowSoftInputMode="stateUnspecified|adjustNothing"
toolbar:itemBackground="@drawable/bg|#00FF00"/>