1. Add a custom property to the new control:
Main steps:
(1) declare the attribute in the Attrs.xml file , with the attribute name: Name and Format: format:
1 <declare-styleablename= "Mytogglebtn">The name of the //Reputation property set, that is, which control the attributes belong to. 2 <attrname= "Current_state"format= "Boolean"/>//Reputation Attribute Current_state format is a Boolean type3 <attrname= "Slide_button"format= "Reference"/>//Reputation Attribute Slide_button format is reference type4 </declare-styleable>
All of the format types, as follows:
1 Reference Reference 2 color 3 boolean 4 Dimension Size value 5float floating-point value 6integer integer value 7 string string 8 enum enumeration value
(2) using the new attribute in the layout file, You must first declare the namespace , such as:
1 Xmlns:heima= "http://schemas.android.com/apk/res/com.himi.togglebtn"
Description
xmlns is the abbreviation for XML name space;
Heima can be any write character
Http://schemas.android.com/apk/res/this for Android fixed format;
COM.ITHEIMA.MYTOGGLEBTN the package name for this app, as consistent in the manifest configuration file.
Layout file:
1 < Com.itheima.mytogglebtn.MyToggleButton 2 Xmlns:heima = "Http://schemas.android.com/apk/res/com.himi.mytogglebtn" 3 android:layout_width= "Wrap_content"4 android:layout_height = "Wrap_content"5 heima:slide_button= "@drawable/slide_button" />
(3) In the construction method of the custom view, the desired property value is obtained by parsing the AttributeSet object .
1 TypedArray ta = context.obtainstyledattributes (attrs, r.styleable.mytogglebtn); // obtained by Attrs Typearray
2. The above procedure is illustrated by a case study as follows:
(1) First create a new "Android" project, named "Custom Attributes", as follows:
(2) Customizing a MyView inherits from view, as follows:
(3) Create a new XML file named Attrs.xml, placed under the Res/values folder, as follows:
Learn what a attr (attribute) format is by looking at the source sdt/platforms/android-14/data/res/values/attrs.xml:
(4) Under the Activity_main.xml file, modify the namespace, using the attributes defined above, as follows:
1 <Relativelayout2 xmlns:android= "Http://schemas.android.com/apk/res/android"3 xmlns:heima= "Http://schemas.android.com/apk/res/com.himi.myattrsdemo" 4 Xmlns:tools= "Http://schemas.android.com/tools"5 Android:layout_width= "Match_parent"6 Android:layout_height= "Match_parent"7 Tools:context= "Com.himi.myattrsdemo.MainActivity" >8 9 <com.himi.myattrsdemo.myviewTen Android:layout_width= "Wrap_content" One Android:layout_height= "Wrap_content" A Android:layout_centerhorizontal= "true" - android:layout_centervertical= "true" - Android:background= "@drawable/ic_launcher" the - heima:test_msg= "@string/app_name" - heima:test_bitmap= "@drawable/ic_launcher" - /> + - </Relativelayout>
(5) in the custom MyView constructor method, the desired property value is obtained by parsing the AttributeSet object:
Custom Controls (views) Period 28 Note 06: Customizing the Control using System controls (custom properties)