Examples of using custom composite controls in Android development _android

Source: Internet
Author: User

One, define an XML layout file
setting_item_view.xml

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Fill_parent "android:layout_height=" 60dip "> <textview android:id=" @+id/tv _title "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:layout_alignparentlef" T= "true" android:layout_marginleft= "5dip" android:layout_margintop= "5dip" android:textcolor= "#000000" an Droid:textsize= "20dip"/> <textview android:id= "@+id/tv_desc" android:layout_width= "Wrap_content" and roid:layout_height= "Wrap_content" android:layout_below= "@+id/tv_title" android:layout_marginleft= "5dip" Androi
    D:layout_marginbottom= "5dip" android:textcolor= "#99000000" android:textsize= "18dip"/> <checkbox Android:clickable= "false" android:focusable= "false" android:id= "@+id/cb_status" android:layout_width= "Wrap_con" Tent "Android:layout_height= "Wrap_content" android:layout_alignparentright= "true" android:layout_centervertical= "true" Android:layout_mar ginright= "20dip"/> <view android:layout_width= "fill_parent" android:layout_height= "0.2dip" android:l Ayout_alignparentbottom= "true" android:layout_alignparentleft= "true" android:layout_marginleft= "5dip" Android:

 layout_marginright= "5dip" android:background= "#000000"/> </RelativeLayout>

Defining attributes in Src/values/attrs.xml

<?xml version= "1.0" encoding= "Utf-8"?>

<resources>
  <declare-styleable name= "TextView" >
    <attr name= "title" format= "string"/>
    <attr name= "desc_on" format= "string"/>
    <attr "Desc_off" format= "string"/>
  </declare-styleable>


</resources>


Third, customize a view to inherit from the layout you need
Iniview (Context context) Initializes a custom layout file

Customize some API methods based on requirements

public class Settingitemview extends Relativelayout {private CheckBox cb_status;
  Private TextView Tv_title;

  Private TextView Tv_desc;
  Private String title;
  Private String desc_on;



  Private String Desc_off;
    public void Iniview {view.inflate (context, R.layout.setting_item_view, this);
    Cb_status = (CheckBox) Findviewbyid (r.id.cb_status);
    Tv_title = (TextView) Findviewbyid (r.id.tv_title);
  Tv_desc = (TextView) Findviewbyid (R.ID.TV_DESC);
    Public Settingitemview (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);
  Iniview (context);
    Public Settingitemview (context, AttributeSet attrs) {Super (context, attrs);
    Iniview (context);
    title = Attrs.getattributevalue ("Http://schemas.android.com/apk/res/com.victor.mobilesafe", "title");
    desc_on = Attrs.getattributevalue ("Http://schemas.android.com/apk/res/com.victor.mobilesafe", "desc_on"); Desc_off = Attrs.getattributevalue ("Http://schemas.android.com/apk/res/com.victor.mobilesafe "," Desc_off ");
    Tv_title.settext (title);
  Setdesc (Desc_off);
    Public Settingitemview {Super (context);
  Iniview (context);

  public Boolean ischecked () {return cb_status.ischecked ();
    public void setchecked (Boolean checked) {if (checked) {Setdesc (desc_on);
    }else{Setdesc (Desc_off);

  } cb_status.setchecked (checked);
  public void Setdesc (String text) {tv_desc.settext (text);

 }

}

Use the custom combination control in a layout file
don't forget to declare a custom namespace
Xmlns:victor= "Http://schemas.android.com/apk/res/com.victor.mobilesafe"

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
  xmlns:victor=" Http://schemas.android.com/apk/res/com.victor.mobilesafe "
  android:layout_ Width= "Match_parent"
  android:layout_height= "match_parent"
  android:orientation= "vertical" >
 < Com.victor.mobilesafe.ui.SettingItemView
    android:id= "@+id/siv_update"
    android:layout_width= "Wrap_ Content "
    android:layout_height=" wrap_content "
    victor:desc_off= Automatic Update shutdown"
    victor:desc_on= "Automatic Update Open"
    victor:title= "Set Automatic Updates" >
  </com.victor.mobilesafe.ui.SettingItemView>
</linearlayout >

Summarize:

1. Customize a view generally speaking, inherit relative layout, or linear layout viewgroup;

2. Implement the method of constructing the parent class. In general, a custom layout file needs to be initialized in the constructor method;

3. Define some API methods according to some needs or requirements;

4. Customize the properties of the control, as needed, to refer to the TextView property;

5. Custom namespaces, for example:

Xmlns:victor= "http://schemas.android.com/apk/res/< Package name >"

xmlns:victor= "http://schemas.android.com/apk/ Res/com.victor.mobilesafe "

6. Customizing our attributes in the Res/values/attrs.xml

7. Use our custom attributes

For example:

 itheima:title= "Set Automatic Update itheima:desc_on=" Set Automatic Update

 is turned on "

 itheima:desc_off=" Set Automatic Update has been turned off "

8. In our custom control's constructor with two parameters AttributeSet Attrs takes out our property values and associates the controls corresponding to the custom layout file.

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.