Custom component learning and Summarization

Source: Internet
Author: User

By combining two textviews with one checkbox and one view into a custom control, you can customize its attributes.

1 first, the layout file set_item_view.xml is provided:

<? 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 = "65dip">

Android: Id = "@ + ID/TV _tittle"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_marginleft = "10dip"
Android: layout_margintop = "10dip"
Android: textcolor = "#000000"
Android: textsize = "20sp"/>

Android: Id = "@ + ID/TV _des"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_below = "@ ID/TV _tittle"
Android: layout_marginleft = "10dip"
Android: textcolor = "#88000000"
Android: textsize = "18sp"/>

Android: clickable = "false"
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: layout_width = "fill_parent"
Android: layout_height = "0.2dip"
Android: layout_alignparentbottom = "true"
Android: layout_marginleft = "5dip"
Android: layout_marginright = "5dip"
Android: Background = "#000000"/>

</Relativelayout>

2. Customize a class, integrate relativelayout, and implement some of the methods.

public class SetItemView extends RelativeLayout {    private CheckBox cb_status;    private TextView tv_tittle;    private TextView tv_des;    private String deson;    private String desoff;    private void initview(Context context) {        View.inflate(context, R.layout.set_item_view, SetItemView.this);        cb_status = (CheckBox) findViewById(R.id.cb_status);        tv_tittle = (TextView) findViewById(R.id.tv_tittle);        tv_des = (TextView) findViewById(R.id.tv_des);    }    public SetItemView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        initview(context);    }    public SetItemView(Context context, AttributeSet attrs) {        super(context, attrs);        initview(context);        String tittle = attrs.getAttributeValue(                "http://schemas.android.com/apk/res/com.djf.mobilesafty",                "tittle");        deson = attrs.getAttributeValue(                "http://schemas.android.com/apk/res/com.djf.mobilesafty",                "deson");        desoff = attrs.getAttributeValue(                "http://schemas.android.com/apk/res/com.djf.mobilesafty",                "desoff");        tv_tittle.setText(tittle);        SetDesc(desoff);    }    public SetItemView(Context context) {        super(context);        initview(context);    }    // 判断组件是否被选中    public boolean isChecked() {        return cb_status.isChecked();    }    // 设置组件状态    public void SetChecked(boolean checked) {        if (checked) {            tv_des.setText(deson);        }        else {            tv_des.setText(desoff);        }        cb_status.setChecked(checked);    }    // 设置描述信息    public void SetDesc(String string) {        tv_des.setText(string);    }}

3. Implement attributes of custom composite controls
First define a custom namespace xmlns: DJF = "http://schemas.android.com/apk/res/com.djf.mobilesafty"

Special attention should be paid to the red font. One is the naming control and the other is the package name.

Then you can customize attributes.

Android: Id = "@ + ID/siv_update"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
DJF: desoff = "automatic upgrade disabled"
DJF: deson = "automatic upgrade enabled"
DJF: tittle = "Set automatic upgrade status">

<Com. DJF. mobilesafty. UI. setitemview
Android: Id = "@ + ID/siv_update"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
DJF: desoff = "the mobile phone anti-theft status has been disabled"
DJF: deson = "the mobile phone anti-theft status has been enabled"
DJF: tittle = "set mobile phone anti-theft status">
</COM. DJF. mobilesafty. UI. setitemview>

The red font above indicates the package name of the custom class above ~~~~

Now, a simple custom component and its attributes are introduced. You can also define and click Properties for the custom component ~~~~

Custom component learning and Summarization

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.