Custom component example: Use the attrs. xml file to customize RadioButton

Source: Internet
Author: User

 

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.