Requirement: When the Dark Horse does security guard, the function 9 sets the central interface as follows:
When you click on item, the check box reverses the status, and automatic Update is turned off changes the content and color.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/71/2A/wKiom1XH0WXiCmIJAADwsfmC-vM169.jpg "title=" Set Center. PNG "alt=" Wkiom1xh0wxicmijaadwsfmc-vm169.jpg "/>
This interface can be found to be like a ListView, but not a ListView, because its item number is fixed, and the last
Item is not the same as before. Although this looks like a standard list structure, the fact that each item is not exactly the same, because
The content of the prompt text for each item (such as "Automatic Update is OFF") is not exactly the same.
If the layout is in a general way, 4 item will have 3*4 = 12 view, there will be more than one Findviewbyid () and
OnClick event. Then the program will appear very bloated and redundant.
If you are using a ListView, 4 items will have 4 views, and you can add item click events to the item item. But
As mentioned above, 4 item is not a fully standard ListView item, each item item's hint text content is independent
Of If you must use the ListView to implement, then as long as the declaration of two arrays to hold the check box two states each
The item's prompt information. Although the ListView can also achieve the desired function, but a bit overqualified feeling, and poor reusability.
In order to solve the above problem, the technique of customizing the composite control appeared.
1. What is a custom combo control
Custom composite controls are more than custom composite control classes. is to encapsulate multiple controls in a single container, adding the entire
Device as a control. All of the controls are also implemented in code, using the corresponding classes.
Features of custom composite controls
1) Low redundancy
2) Strong reusability
2. Customizing the implementation of a composite control
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/71/2A/wKiom1XH2eHBhe3lAAA3isPFdWQ887.jpg "title=" Set Center. PNG "alt=" Wkiom1xh2ehbhe3laaa3ispfdwq887.jpg "/>
think of each item as a custom composite control that should have a title, desc_on, Desc_off, check four properties.
It is more suitable for management and control to encapsulate the information of prompt Text property in different states in the control.
1) Design the layout of the custom control
<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android= "http://schemas.android.com/ Apk/res/android " android:layout_width=" Match_parent " android:layout_height=" Match_parent " android:background=" # F0f0f0 " > <!-- Title --> <TextView android:id= "@+id/tv_title" android:layout_width= "Match_parent" android:layout_height= "Wrap_ Content " Android:layout_alignparenttop= "true" android:layout_marginleft= "5DP" android:layout_margintop= "5DP" android:text= "title" android:textsize= "20DP" android:textstyle= "Bold" / > <!-- Description --> <TextView android:id= "@+id/tv_desc" android:layout_width = "Match_parent" android:layout_height= "Wrap_content" android:layout_alignleft= "@id/tv_title" android:layout_below= "@id/tv_title" android:layout_margintop= " 5DP " android:text= "description" android:textsize= "15DP" / > <!-- check box --> <!-- Set the following two properties to False to block the Click event for the check box. Because I want to control the change of the check box by clicking the entire custom control. --> <!-- android: Clickable= "false" --> <!-- android:focusable= "false" --> <CheckBox android:id= "@+id/cb_check" android:layout_width= "Wrap_content" android:layout_ height= "Wrap_content" android:layout_alignparentright= "true" android:layout_marginright= "5DP" android:clickable= "false" android:focusable= "false" /> </relativelayout >
Layout effect:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/71/2B/wKiom1XH3BiT9CSIAAAPVhBVgNc397.jpg "title=" Item layout . PNG "alt=" Wkiom1xh3bit9csiaaapvhbvgnc397.jpg "/>
2) Create a custom control class to inherit the layout
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/71/27/wKioL1XH3vmzJcsUAAA2575xqrg318.jpg "title=" Custom control class . PNG "alt=" Wkiol1xh3vmzjcsuaaa2575xqrg318.jpg "/>
3) Create a custom namespace file for the project
System's Namespace
Each layout must have 650) this.width=650; "Src= " http://s3.51cto.com/wyfs02/M01/71/2B/ Wkiom1xh4alguo0xaaa4i9msie4365.jpg the "title=" System namespace. PNG "alt=" wkiom1xh4alguo0xaaa4i9msie4365.jpg "/>,
In fact, this attribute points to a system's own Attr.xml file, in the system's Values folder directory, it specifies what the control
What properties will be available.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/71/2B/wKiom1XH4NnBmXYcAACTJp5Y_2w714.jpg "title=" Attrs. PNG "alt=" wkiom1xh4nnbmxycaactjp5y_2w714.jpg "/>
The system comes with a Attr.xml file that defines the properties of all the controls that the system comes with.
the root node label of the system Attr.xml file
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/71/27/wKioL1XH5YbgaVTaAABrn3WbrYs247.jpg "title=" root point. PNG "alt=" Wkiol1xh5ybgavtaaabrn3wbrys247.jpg "/>
" such as the ImageView control
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/71/2B/wKiom1XH4xjBKpb8AALdoC-ZST8807.jpg "title=" attrs2 . PNG "alt=" Wkiom1xh4xjbkpb8aaldoc-zst8807.jpg "/>
controls are declared with the <declare-styleable> tag, and the properties of the control are specified with the <attr> tag.
Custom Namespaces
" refer to the system's custom namespace file format and create a new one in your own project under the Values folder
Attr.xml files of the same name
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/71/27/wKioL1XH5yazcl5sAAIpgVPlF_I016.jpg "title=" New Attrs. PNG "alt=" Wkiol1xh5yazcl5saaipgvplf_i016.jpg "/>
When you create a new Attr.xml file in the values directory, you will automatically be prompted with the root tag resources,
The system comes with the same Attr.xml file format.
" declares custom controls and their properties
This article is from the "line of the World" blog, please be sure to keep this source http://4259297.blog.51cto.com/4249297/1683192
Implementation of the Android custom combo control