Android development-custom UI components and attributes, androidui

Source: Internet
Author: User

Android development-custom UI components and attributes, androidui

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/ <package name>" xxx is scheam name
 
6. customize our attributes and define the attributes in res/values/attrs. xml (create an attribute file ).

Similar: </p> <p>
<? Xml version = "1.0" encoding = "UTF-8"?> <Resources>
<Declare-styleable name = "TextView">
<! -- Custom control attributes -->
<Attr name = "desc_on" format = "string"/>
<Attr name = "desc_off" format = "string"/>
<Attr name = "titles" format = "string"/>
</Declare-styleable> </resources> <
 
7. Use custom attributes. </P> <p>
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


<? 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 = "68dip"> <TextView android: id = "@ + id/TV _title" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginLeft = "10dip" android: layout_marginTop = "8dip" android: text = "set whether to automatically update" android: textColor = "#000000" android: textSize = "20sp"/> <TextView android: id = "@ + id/TV _desc" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_below = "@ id/TV _title" android: layout_marginLeft = "10dip" android: text = "auto update disabled" android: textColor = "#88000000" android: textSize = "18sp"/> <CheckBox android: id = "@ + id/cb_status" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignParentRight = "true" android: layout_centerVertical = "true" android: layout_marginRight = "10dip"/> <View android: clickable = "false" android: layout_width = "match_parent" android: layout_height = "0.2dip" android: layout_alignParentBottom = "true" android: layout_marginLeft = "5dip" android: layout_marginRight = "5dip" android: background = "#000000"/> </RelativeLayout>


2. Call it in the corresponding Activity layout File

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: andy = "http://schemas.android.com/apk/res/com.andy.mobilesafe" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <com. andy. mobilesafe. ui. settingItemView android: id = "@ + id/siv_update" android: layout_width = "wrap_content" android: layout_height = "wrap_content" andy: desc_off = "setting auto update disabled" andy: desc_on = "Set automatic update enabled" andy: titles = "Set automatic update"/> </LinearLayout>

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


<? Xml version = "1.0" encoding = "UTF-8"?> <Resources> <declare-styleable name = "TextView"> <! -- Custom control attributes --> <attr name = "desc_on" format = "string"/> <attr name = "desc_off" format = "string"/> <attr name = "titles" format = "string"/> </declare-styleable> </resources>

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 );}}




Related Article

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.