After analysis, the above button effect is actually a layout, one of the simplest vertical linear layout, the upper part is a ImageView, the next part is a TextView, this layout can be clicked, Monitoring can be set.
We will first write our own ImageButton class, then write the layout for our custom Button in the main layout file , and finally in the Activity Call our custom ImageButton .
First, we write a Myimagebutton class that inherits from the linearlayout
Package Com.billhoo.study; Import Android.content.Context; Import Android.widget.ImageView; Import Android.widget.LinearLayout; Import Android.widget.TextView; //custom ImageButton, simulates ImageButton, and displays text underneath it//provides a partial interface to a button Public classMyimagebutton extends LinearLayout { PublicMyimagebutton (Context context,intImageresid,intTextresid) {Super (context); Mbuttonimage=NewImageView (context); Mbuttontext=NewTextView (context); Setimageresource (IMAGERESID); Mbuttonimage.setpadding (0,0,0,0); SetText (TEXTRESID); SetTextColor (0xff000000); Mbuttontext.setpadding (0,0,0,0); //set properties for this layoutSetclickable (true);//can be clickedSetfocusable (true);//can focusSetbackgroundresource (Android. R.drawable.btn_default);//layout is only used with the background of the normal buttonSetOrientation (linearlayout.vertical);//Vertical Layout//Add the image first, and then add the text//adding the order will affect the layout effectAddView (mbuttonimage); AddView (Mbuttontext); } //----------------Public Method----------------------------- /** Setimageresource Method*/ Public voidSetimageresource (intresId) {Mbuttonimage.setimageresource (resId); } /** SetText Method*/ Public voidSetText (intresId) {Mbuttontext.settext (resId); } Public voidSetText (charsequence buttontext) {mbuttontext.settext (ButtonText); } /** SetTextColor Method*/ Public voidSetTextColor (intcolor) {Mbuttontext.settextcolor (color); } //----------------Private attribute----------------------------- PrivateImageView Mbuttonimage =NULL; PrivateTextView Mbuttontext =NULL; }
Then write the XML layout for our custom button in the main layout , and note that our "button" is actually a linear layout, so XML should be written in LinearLayout and not Button or ImageButton
<?xml version="1.0"encoding="Utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="Vertical"Android:layout_width="fill_parent"Android:layout_height="fill_parent"> <!--This is our data management button--<linearlayout android:id="@+id/ll_bt_data_config"Android:layout_width="wrap_content"android:layout_height="wrap_content"/> </LinearLayout>
Finally, set the listener for our custom buttons in Activity
Package Com.billhoo.study; Import android.app.Activity; Import Android.os.Bundle; Import Android.view.View; Import Android.widget.Button; Import Android.widget.LinearLayout; Public classTestactivity extends Activity {/** Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); Btdataconfig=NewMyimagebutton ( This, R.drawable.option, R.string. Text); //get the container for the parcel buttonLlbtdataconfig =(LinearLayout) Findviewbyid (r.id.ll_bt_data_config); //Add our custom button into this containerLlbtdataconfig.addview (btdataconfig); //setting the monitor for a buttonBtdataconfig.setonclicklistener (NewButton.onclicklistener () {@Override Public voidOnClick (View v) {Btdataconfig.settext ("the button has been clicked."); } }); } PrivateLinearLayout Llbtdataconfig =NULL;//The container that wraps this button in the main layout PrivateMyimagebutton Btdataconfig =NULL; }
Effect:
Android custom Android with pictures and text ImageButton