First, on the view and layout,
can see:
- 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;
- ViewGroup is a special view that can contain many sub-view and sub-viewgroup, which is a container for placing controls and layouts;
- 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;
- 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