Android Custom XML Properties

Source: Internet
Author: User

<?XML version= "1.0" encoding= "Utf-8"?><Resources>    <declare-styleablename= "Ratiolayout">        <attrname= "Picratio"format= "float" />        <attrname= "relative">            <enumname= "width"value= "0" />            <enumname= "height"value= "1" />        </attr>    </declare-styleable></Resources>

There are two typical configuration of XML attributes, which need to be written in the Values-attrs file.

The first is name, followed directly by a type, which can be configured arbitrarily in XML.

The second is an enumeration so that only fixed properties can be selected.

So, the question is, how do you read these properties in the view?? Two methods are described below.

1.

        < Com.itheima.googleplay_8.views. Ratiolayout            xmlns:itheima= "Http://schemas.android.com/apk/res-auto"            Android : layout_width= "Fill_parent"            android:layout_height= "Wrap_content"             itheima:relative= "width"            itheima:picratio= "2.43"  >
     Public Ratiolayout (Context context, AttributeSet attrs) {        Super(context, attrs);         = context.obtainstyledattributes (attrs, r.styleable.ratiolayout);         = Typedarray.getfloat (r.styleable.ratiolayout_picratio, 0);         = Typedarray.getint (r.styleable.ratiolayout_relative, relative_width);        Typedarray.recycle ();    }

2.

    < Com.example.myview.MyView         Android:id = "@+id/myview1"         android:layout_width= "Wrap_content"        android:layout_height= "Wrap_ Content "        yueqing:content=" Contents "        yueqing:title=" title " >    </ Com.example.myview.MyView >
 PublicMyView (Context context, AttributeSet attrs) {Super(context, attrs); View Inflate= View.inflate (Context, R.layout.myview,NULL); CheckBox CB=(CheckBox) Inflate.findviewbyid (R.ID.CB); TextView TV=(TextView) Inflate.findviewbyid (r.id.tv); String content=Attrs.getattributevalue ("Http://schemas.android.com/apk/res/com.example.myview",                "Content"); if(!cb.ischecked ())        {tv.settext (content); }                 This. AddView (inflate); intAttributecount =Attrs.getattributecount (); System.out.println (Attributecount+ ">>>>>>>>>>>>>>>>"); }

Android Custom XML Properties

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.