Android custom widget
Custom Controls are difficult for android programmers to break through, at least for me, however, we can find some good blogs on the internet, take custom controls, study and study them, learn more, write more, and understand some principles, let's talk about custom composite controls today. This is especially suitable for the title bar or the setting interface. See the following figure:
It is very suitable for the use of composite controls, and now write a play:
Activity_main.xml
SettingView.java
Public class SettingView extends RelativeLayout {private TextView title; private TextView content; public SettingView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle ); initView (context);} public SettingView (Context context, AttributeSet attrs) {super (context, attrs); initView (context ); /*** get a style property set **: Creates a ing relationship between the obtained style set attrs in the xml layout file and the custom style Set */TypedArray a = context. obtainStyledAttributes (attrs, R. styleable. settingView); String title =. getString (0); String content =. getString (1); // set to setTitle (title); setContent (content);. recycle ();} public SettingView (Context context) {super (context); initView (context);} private View initView (Context context) {View view = View. inflate (context, R. layout. setting_view, this); title = (TextView) view. findViewById (R. id. title); content = (TextView) view. findViewById (R. id. content); return view;} public void setTitle (String tilte) {title. setText (tilte);} public void setContent (String strContent) {content. setText (strContent);} public void setTextSize (int size) {content. setTextSize (size); title. setTextSize (size );}}
Setting_view.xml
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: orientation = "vertical"
Android: background = "#22000000"
>
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
>
Android: id = "@ + id/title"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: textSize = "20sp"
Android: layout_marginTop = "5dp"
Android: layout_marginLeft = "5dp"
/>
Android: id = "@ + id/content"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: textSize = "16sp"
Android: layout_marginLeft = "5dp"
Android: layout_below = "@ id/title"
/>
Android: id = "@ + id/cb"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignParentRight = "true"
Android: layout_centerVertical = "true"
/>
Android: layout_width = "fill_parent"
Android: layout_height = "1px"
Android: background = "#000000"
Android: layout_below = "@ id/cb"
/>
We found that activity_main.xml has two attributes.
Hy: title = "title 1"
Hy: content = "content 1"
An error is reported in the xml file.
Error: Error parsing XML: unbound prefix: there is no such prefix as hy, because we write android: What attributes are caused by xmlns: android =
Http://schemas.android.com/apk/res/android"
Such a namespace is in,
Then we also customize a namespace, just write it like aboveXmlns: hy =
Http://schemas.android.com/apk/res/Com. example. customcircle "// you need the package name after res.
An error is reported in the xml file: Multiple annotations found at this line:-error: No resource identifier found for attribute 'content' in package 'com. example. mcmcircle'
The definition of conent is not found in our package.
WhileHy: title =
"Title"Hy: content =
"Content"
Is my custom property
If you want to use custom attributes, You need to define these attributes. Why is the system's android: attribute name --- OK? That's because the android sdk has been declared. Find the sdk we are using.D: \ java \ tools \ adt-bundle-windows-x86_64-20130917 \ sdk \ platforms \ android-10 \ data \ res \ values has an attrs under this directory. xml file, we will find a common
This is the property in TextView, so we need to imitate it to create an attrs file under the value directory.
Declare-styleable indicates the declared Control name. "SettingView"> When saved, an internal class will be generated in your R. java file.
Public
Static
Final
ClassAttr {// property set} And then declare the property "title"Format =
"String"> "Content"Format =
"String">Name indicates the name format of the property.
Then let's save the code and there will be no error prompt. Then we can look at the R. java file again.
Public
Static
Final
ClassAttr {/**
Must be a string value, using '\;' to escape characters such as '\ n' or '\\Uxxxx'ForUnicodeCharacter.
This may also be a reference to a resource (in the form"@[ package :]type : name
") Ortheme attribute (in the form"?[ package :][type :] name
") Containing a value of this type .*/Public Static Final Int Content= 0x7f010001 ;/**
Must be a string value, using '\;' to escape characters such as '\ n' or '\\Uxxxx'ForUnicodeCharacter.
This may also be a reference to a resource (in the form"@[ package :]type : name
") Ortheme attribute (in the form"?[ package :][type :] name
") Containing a value of this type .*/Public Static Final Int Title= 0x7f010000;} at this time, we ran our project and found that the custom property did not work. That means we need to display it in code.
The AttributeSet class indicates the attributes defined in the property set, for example, android: id ="@ + Id/sv1"Android: layout_width ="Fill_parent"Android: layout_height ="Wrap_content"Hy: title ="Title"Hy: content ="Content"This is equivalent to encapsulating these attributes into a class, which is really powerful in java and object-oriented programming.
However, this operation is performed in the constructor.
PublicSettingView (Context context, AttributeSet attrs ){Super(Context, attrs); initView (context);/*** get a style Attribute Set * put the layout FileXmlThe obtained style set inAttrsCreate a ing relationship with a custom style Set */TypedArray a = context. obtainStyledAttributes (attrs, R. styleable.SettingView); String title =. getString (0); String content =. getString (1); // set to setTitle (title); setContent (content);. recycle ();}.
Running effect:
To sum up the steps for custom composite controls:
1. Write a class to inherit the ViewGroup LinearLayout RelativeLayout
2. If you define a view object in the layout file, use the constructor of the two parameters of this view object.
3. Define the content to be displayed in the Custom Control
View. inflate (context, R. layout. ui_setting_view, this );
4. Add attributes of the custom control.
Namespace for Custom Attributes
5. Declare Custom Attributes
Observe the R file generation attr internal class generation styleable array all attrs
6. Configure custom attributes in the xml layout file.
7. Get AttributeSet In the constructor of the custom view object
Establishes a ing relationship with custom properties.
8. Set the layout file data in the Custom Composite Control and expand the click event.
9. Use a custom view object in the layout file.