The original attributes of Android may not meet our current needs. After all, some people may come up with something that android will not do. Today, I will briefly write down how to define custom properties for custom controls. It seems a bit dizzy to see this description. In turn, the control and attributes are both custom. Ha ~
The above is the running interface, which has two custom buttons, mainly used for distinguishing.
Firstres/values/
Create an attrs. xml file in the directory and use the Custom Attributes
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="MyCustomWidget"> <attr name="myText" format="string"/> <attr name="isEnable" format="boolean"/> </declare-styleable></resources>
The name value of the tag declare-styleable is that you can use this value to obtain relevant attributes, which indicates that these attributes are assigned to custom controls.
The following code reads the set value from the XML Attribute in the constructor of mymwmwidget. java. You can use the obtained value to do your own work. The Code is as follows:
Package NBE. sense7.vinci. custom. xmlattrs; import android. content. context; import android. content. res. typedarray; import android. util. attributeset; import android. widget. button; public class customwedgit extends button {public customwedgit (context, attributeset attrs) {super (context, attrs); // todo auto-generated constructor stub typedarray A = context. obtainstyledattributes (attrs, R. styleable. mycustomwidget); Final int indexcount =. getindexcount (); For (INT I = 0; I <indexcount; ++ I) {int ATTR =. getindex (I); Switch (ATTR) {case R. styleable. mycustomwidget_mytext: // obtain the mytext attribute value string mytext =. getstring (ATTR); // after obtaining the value, you can use it as you want. Here I will display the customwedgit text for the button setting button. this. settext (mytext); break; case R. styleable. mycustomwidget_isenable: Boolean isenable =. getboolean (ATTR, false); // use the Boolean attribute value to do the break you want to do here;}. recycle ();}}
Finally, add the custom attributes to layout. Main. xml:
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns: Vinci = "http://schemas.android.com/apk/res/nbe.sense7.vinci.custom.xmlattrs" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: orientation = "vertical"> <NBE. sense7.vinci. custom. xmlattrs. customwedgit Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "the value set by the text attribute of the button"/> <NBE. sense7.vinci. custom. xmlattrs. customwedgit Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Vinci: mytext = "button custom mytext attribute setting value"/> </linearlayout>
The most important thing is that you should not forget to add a dark line above. xmlns: you can add a custom name to the end. My name is vinici. The value in the quotation marks should be xmlns: vinci = "http://schemas.android.com/apk/res/your package name ".
OK ~ Hand-written !!