"Android first line code" learning record 008-Create a custom control

Source: Internet
Author: User

First, on the view and layout,

can see:

    1. View is the most basic UI component in Android, which can draw a rectangular area on the screen and respond to events in the region;
    2. ViewGroup is a special view that can contain many sub-view and sub-viewgroup, which is a container for placing controls and layouts;
    3. All the controls we use are directly or indirectly inherited from the view, and the various controls actually add their own functionality on the basis of the view;
    4. All layouts are directly inherited from ViewGroup;

Second, define the custom control needs:

    • The layout file of the custom control;
    • The class of the custom control;

To customize a title bar as an example, the effect is (please disregard its unsightly degree)

Custom layout file:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:background= "#444"android:orientation= "Horizontal" >    <ButtonAndroid:id= "@+id/btnback"Android:layout_width= "33dip"Android:layout_height= "33dip"Android:background= "@drawable/icon_back" />    <TextViewAndroid:id= "@+id/tvtitle"Android:layout_width= "0dip"Android:layout_height= "33dip"Android:layout_weight= "1"Android:background= "#444"android:gravity= "Center"Android:text= "title bar"Android:textcolor= "#fff" />    <ButtonAndroid:id= "@+id/btnedit"Android:layout_width= "33dip"Android:layout_height= "33dip"Android:background= "@drawable/icon_edit" /></LinearLayout>

Classes for custom controls:

 Packagecom.matclone.CustomUIViewTest;Importandroid.app.Activity;ImportAndroid.content.Context;ImportAndroid.util.AttributeSet;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.widget.Button;Importandroid.widget.LinearLayout;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast; Public classTitleBarextendsLinearLayout {PrivateButton Btnback =NULL; PrivateTextView Tvtitle =NULL; PrivateButton Btnedit =NULL;  PublicTitleBar (Context context, AttributeSet attrs) {Super(context, attrs); //TODO auto-generated Constructor stubLayoutinflater.from (context). Inflate (R.layout.title_bar, This); Btnback= (Button) This. Findviewbyid (R.id.btnback); Tvtitle= (TextView) This. Findviewbyid (R.id.tvtitle); Btnedit= (Button) This. Findviewbyid (R.id.btnedit); Btnback.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method Stub( Activity) GetContext ()). Finish ();                }        }); Btnedit.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubToast.maketext (GetContext (), "You clicked the Edit button", Toast.length_short). Show ();    }        }); }         Public voidSettitle (String title) { This. Tvtitle.settext (title); }}

Third, using a custom control requires:

    • Introduce a custom control in the layout file;

To introduce a custom control in another active layout file:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" >    <Com.matclone.CustomUIViewTest.TitleBarAndroid:id= "@+id/titlebar"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" >    </Com.matclone.CustomUIViewTest.TitleBar></LinearLayout>

"Android first line code" learning record 008-Create a custom 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.