1. Create attrs. xml under res/values
<Declare-styleable name = "MyRadioButton">
<Attr name = "str" format = "string"/>
</Declare-styleable>
MyRadioButton is the component name. The attr tag defines the attributes of the component. The name corresponds to the attribute name and the format is the attribute type. For details, see [Android] attrs. format of the format value of the attribute type in the xml file.
2. Use the attrs. xml file definition in custom components
Public class MyRadioButton extends RadioButton {
Private String url;
Public MyRadioButton (Context context, AttributeSet attrs ){
Super (context, attrs );
TypedArray taArray = context. obtainStyledAttributes (attrs, R. styleable. MyRadioButton );
This. url = taArray. getString (R. styleable. MyRadioButton_str );
TaArray. recycle ();
}
Public String getUrl (){
Return url;
}
Public void setUrl (String url ){
This. url = url;
}
}
A. TypedArray is a set of attributes specified by R. styleable. MyRadioButton.
B. Get the property value through getXXX.
C. recycle () ends binding
3. Use it in the layout File
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: demo = "http://schemas.android.com/apk/res/net.csdn.blog.wxg630815"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<RadioGroup
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
>
<Net. csdn. blog. wxg630815.MyRadioButton
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: id = "@ + id/myradio1"
Demo: str = "1.csdn.net"
/>
<Net. csdn. blog. wxg630815.MyRadioButton
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_parent"
Android: id = "@ + id/myradio2"
Demo: str = "2.csdn.net"
/>
</RadioGroup>
</LinearLayout>
Note: xmlns: demo = "http://schemas.android.com/apk/res/net.csdn.blog.wxg630815"
Only after this sentence is declared will the url attribute be recognized by the layout file. Net. csdn. blog. wxg630815 indicates the package attribute value of the manifest element in the AndroidManifest. xml file.
Use demo: str to assign values to the url.
The line is on the road