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:
?
1 2 3 4, 5 6 7 8 9 10 11 12 13 14 15 |
<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 redraw it
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The |
|
And then call in the layout file:
?
1 2 3 4 5 6 7 8 9 10 |
<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:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20-21 |
<?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:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The |
|
You can then invoke it in the XML file that you want:
?
1 2 3 4 5 6 |
<com.test.imagetextbutton1 android:id= "@+id/bt1" android:layout_width= "Wrap_content" Wrap_content "android:background=" @drawable/button_bg "/> |
To be used in the activity again:
?
1 2 3 4 5 6 7 8 9 10 |
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.