This article describes the Android implementation of custom with text and Picture button methods. Share to everyone for your reference. The specific analysis is as follows:
In Android development, it's often necessary to use a button with text and pictures to explain the common implementations.
The implementation of the button with the system itself
The easiest way to do this is to use the button that comes with the system, which is the smallest amount of code. In the button's property is a drawableleft, this property can set the picture on the left side of the text, but this way must make the icon background color is transparent, if the icon background color is not transparent, will cause the click of the button icon part of the background color will not change.
Main code:
<button
android:id= "@+id/bt3"
android:layout_margintop= "4DP"
android:layout_width= "Wrap_content" "
android:layout_height=" wrap_content "
android:text=" Train "
android:textsize=" 16sp "
android: Textcolor= "#000000"
android:paddingleft= "5DP"
android:paddingtop= "5DP"
android:paddingright= "5DP "
android:paddingbottom=" 5DP "
android:drawableleft=" @drawable/line_bus_icon "
android:background = "@drawable/button_bg" >
</Button>
Implementation effect:
If you want to make the text below the icon, change to Drawabletop can be.
two. Inherit the button of the system and then redraw the
Package com.test;
Import Android.content.Context;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.graphics.Canvas;
Import Android.util.AttributeSet;
Import Android.widget.Button;
public class ImageTextButton2 extends Button {private int resourceid = 0;
Private Bitmap Bitmap;
Public ImageTextButton2 {super (context,null);
} public ImageTextButton2 (context Context,attributeset AttributeSet) {Super (context, attributeset);
This.setclickable (TRUE);
ResourceID = R.drawable.icon;
Bitmap = Bitmapfactory.decoderesource (Getresources (), ResourceID);
public void SetIcon (int resourceid) {this.bitmap = Bitmapfactory.decoderesource (Getresources (), ResourceID);
Invalidate (); @Override protected void OnDraw (Canvas Canvas) {//TODO auto-generated Method Stub//picture at the top of the center show int x =
(This.getmeasuredwidth ()-bitmap.getwidth ())/2;
int y = 0; Canvas.drawbitmap (bitmap, x, Y, Null); The coordinates need to be converted because the text in the button is centered by default. Canvas.translate (0, (This.getmeasuredheight ()/2)-(int) This.gette
Xtsize ());
Super.ondraw (canvas);
}
}
And then call in the layout file:
<com.test.imagetextbutton2
android:id= "@+id/bt2"
android:layout_margintop= "10DP"
android:text= " Hello "
android:textsize=" 15DP "
android:textcolor=" "#000000"
android:layout_width= "60DP"
Android : layout_height= "70DP"
android:background= "@drawable/button_bg"
/>
Note that when invoked in an XML file, you must not use wrap_content for the Layout_width and layout_height two properties, or it will cause the button to display only the text part.
Three. Inherit layout file
Analysis found that a button with text and icon can actually be viewed as a linear layout or relative layout, so you can inherit the layout to implement it.
First implement a button's layout file Img_text_bt.xml:
<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android=
"http://" Schemas.android.com/apk/res/android "
android:layout_width=" wrap_content "
android:layout_height=" Wrap_ Content ">
<imageview
android:id=" @+id/imgview "
android:layout_alignparenttop=" true "
Android:layout_width= "Wrap_content"
android:layout_height= "wrap_content"
android:layout_ Centerinparent= "true"
android:src= "@drawable/icon" >
</ImageView>
<textview
Android:id= "@+id/textview"
android:layout_width= "wrap_content"
android:layout_height= "Wrap_content"
android:layout_centerinparent= "true"
android:layout_below= "@id/imgview" >
</TextView>
</RelativeLayout>
Then to inherit the Relativelayout layout:
Package com.test;
Import Android.content.Context;
Import Android.util.AttributeSet;
Import Android.view.LayoutInflater;
Import Android.widget.ImageView;
Import Android.widget.RelativeLayout;
Import Android.widget.TextView;
public class ImageTextButton1 extends Relativelayout {private ImageView imgview;
Private TextView TextView;
Public ImageTextButton1 {super (context,null);
} public ImageTextButton1 (context Context,attributeset AttributeSet) {Super (context, attributeset);
Layoutinflater.from (context). Inflate (R.LAYOUT.IMG_TEXT_BT, this,true);
This.imgview = (ImageView) Findviewbyid (R.id.imgview);
This.textview = (TextView) Findviewbyid (R.id.textview);
This.setclickable (TRUE);
This.setfocusable (TRUE);
public void Setimgresource (int resourceid) {this.imgView.setImageResource (ResourceID);
public void SetText (String text) {this.textView.setText (text); public void SetTextColor (int color) {This.textview. SetTextColor (color);
public void settextsize (float size) {this.textView.setTextSize (size);
}
}
You can then invoke it in the XML file that you want:
<com.test.imagetextbutton1
android:id= "@+id/bt1"
android:layout_width= "Wrap_content"
android: layout_height= "Wrap_content"
android:background= "@drawable/button_bg"
/>
To be used in the activity again:
BT1 = (ImageTextButton1) Findviewbyid (R.ID.BT1);
Bt1.settext ("icon");
Bt1.settextcolor (Color.rgb (0, 0, 0));
Bt1.setonclicklistener (New Onclicklistener () {
@Override public
void OnClick (View v) {
//TODO auto-generated method Stub
toast.maketext (Mainactivity.this, "Bt1 was clicked", Toast.length_short). Show ();
Three different ways to run the final effect:
Full instance code click here to download the site.
I hope this article will help you with your Android program.