<?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