Two ways to customize a control

Source: Internet
Author: User

First Type: Code

Setting_view.xml

<?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= "55DP" >

<textview

Android:id= "@+id/tv_title"

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

Android:text= "Attribution to display Settings"

Android:textsize= "20sp"/>

<textview

Android:id= "@+id/tv_content"

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

android:layout_below= "@id/tv_title"

Android:text= "The place of attribution has been opened"

Android:textsize= "16sp"/>

<checkbox

Android:id= "@+id/cb_status"

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

Android:layout_alignparentright= "true"

Android:layout_centerinparent= "true"

Android:checked= "true"/>

</RelativeLayout>

public class Mainactivity extends Activity {

@Override

protected void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Settingview SV = new Settingview (this);

Setcontentview (SV); }

}

public class Settingview extends Relativelayout {

Public Settingview (context context, AttributeSet attrs, int defstyle) {

Super (context, attrs, Defstyle);

Initui (context); }

Public Settingview (context context, AttributeSet Attrs) {

Super (context, attrs);

Initui (context); }

Public Settingview (Context context) {

Super (context);

Initui (context); }

private void Initui (context context) {

View view = view.inflate (context, r.layout.setting_view, null);

This.addview (view);//this refers to the object that invokes the AddView (view) method, which is the parameter context}

}

Second way: XML format

Two ways to customize a control

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.