Android custom Android with pictures and text ImageButton

Source: Internet
Author: User

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

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.