Custom UI components and attributes for Android Development

Source: Internet
Author: User

Custom UI components and attributes for Android Development

Although the Android system comes with many components, it must meet our personalized needs. Therefore, for convenience of development, we need to customize Android UI components to meet our personalized needs.
To customize a widget, follow these steps:


1. to customize a View, you must inherit the subclass of ViewGroup such as relative layout and linear layout. ViewGroup is a container of other controls that can be used to place various components.


2. implement three constructor methods of the parent class. Generally, you need to initialize the custom Layout file in the constructor.
One Parameter constructor: used by the new control
Two Parameter creation Methods: Used in calling layout files
Create three parameters: Pass the layout file with a style


3. Define some API methods as needed.

4. Customize the properties of the control as needed. You can refer to TextView attribute writing.

5. Customize the namespace.
Xmlns: xxx = "http://schemas.android.com/apk/res/ <包名> "Xxx is named scheam.

6. customize our attributes and define the attributes in res/values/attrs. xml (create an attribute file ).

For example:








<

7. Use custom attributes.


For example:
Andy: desc_off = "setting auto update disabled"
Andy: desc_on = "setting auto update enabled"
Andy: titles = "Set automatic update"

8. In the constructor with two parameters of the custom control, AttributeSet attrs retrieves the custom property value and associates it with the control corresponding to the custom Layout file.

The code example is as follows:


1. Define a custom control: setting_item_view.xml layout File


     
      
      
      
  
 


2. Call it in the corresponding Activity layout File

 
    
  
 

3. Custom Attributes: res/values/attrs. xml


 
     
          
                               
  
 

4. Implement custom components, inherit the subclass of ViewGroup, and implement constructor and corresponding API methods.

Package com. andy. mobilesafe. ui; import com. andy. mobilesafe. r; import android. content. context; import android. util. attributeSet; import android. view. view; import android. widget. checkBox; import android. widget. relativeLayout; import android. widget. textView;/*** @ author Zhang, tianyou * @ version November 15, 2014 10:22:50 ** custom composite control two textviews one checkbox one View */public class SettingItemView extends RelativeLayout {private CheckBox cb_status; private TextView TV _title; private TextView TV _desc; private String desc_on; private String desc_off;/***** initialize the layout file ** @ param context */private void initView (Context context) {// The second is the layout file root. The third parameter is the layout file parent class. // load a layout File View in SettingItemViewView. inflate (context, R. layout. setting_item_view, this); // View has loaded the SettingItemViewcb_status = (CheckBox) this. findViewById (R. id. cb_status); TV _desc = (TextView) this. findViewById (R. id. TV _desc); TV _title = (TextView) this. findViewById (R. id. title);} public SettingItemView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle ); // This is a constructor that can pass a style call initView (context);}/*** with two parameters, call ** @ param context * @ param attrs * to obtain the attribute value */public SettingItemView (Context context, AttributeSet attrs) {super (context, attrs ); // custom Layout use the called constructor attrs to use initView (context) in the configured attribute layout file; String titles = attrs. getAttributeValue ("http://schemas.android.com/apk/res/com.andy.mobilesafe", "titles"); desc_off = attrs. getAttributeValue ("http://schemas.android.com/apk/res/com.andy.mobilesafe", "desc_off"); desc_on = attrs. getAttributeValue ("http://schemas.android.com/apk/res/com.andy.mobilesafe", "desc_on"); TV _title.setText (titles); setDesc (desc_off);} public SettingItemView (Context context) {super (context ); // use initView (context);}/*** to check whether the composite control has been selected ** @ return */public boolean isChecked () {return cb_status.isChecked ();}/*** set the combination control to select ** @ param checked */public void setChecked (boolean checked) {if (checked) {setDesc (desc_on);} else {setDesc (desc_off);} cb_status.setChecked (checked );} /*** set the description of the Composite Control ** @ param text */public void setDesc (String text) {TV _desc.setText (text );}}




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.