Button implements text-to-text mixing, and button implements text-to-text
Button to achieve text-and-text Mixing
I. Introduction
This article introduces two ways to mix text and text
1. android: drawableTop = "@ drawable/star" to display images in text
Of course there are up, down, and so on.
2. Use ImageSpan of SpannableString to achieve image/Text Mixing
Ii. code example
:
1234 is the first method used
The second method used below
Code:
Fry. Activity02
1 package fry; 2 3 import com. example. buttonDemo1.R; 4 5 import android. app. activity; 6 import android. graphics. bitmapFactory; 7 import android. OS. bundle; 8 import android. text. spannable; 9 import android. text. spannableString; 10 import android. text. style. imageSpan; 11 import android. view. motionEvent; 12 import android. view. view; 13 import android. view. view. onClickListener; 14 import android. view. view. onFocusChangeListener; 15 import android. view. view. onTouchListener; 16 import android. widget. button; 17 18 public class Activity02 extends Activity {19 private Button btn_big; 20 @ Override21 protected void onCreate (Bundle savedInstanceState) {22 // TODO Auto-generated method stub23 super. onCreate (savedInstanceState); 24 setContentView (R. layout. activity02); 25 btn_big = (Button) findViewById (R. id. btn_big); 26 27 // added image 28 to the left of SpannableString spannableString1 = new SpannableString ("left"); 29 ImageSpan = new imageSpan (this, BitmapFactory. decodeResource (getResources (), R. drawable. image_left); 30 spannableString1.setSpan (imageSpan, 0, 4, Spannable. images); 31 32 // added image 33 to the right of image SpannableString spannableString2 = new SpannableString ("right"); 34 ImageSpan imageSpan2 = new ImageSpan (this, BitmapFactory. decodeResource (getResources (), R. drawable. image_right); 35 spannableString2.setSpan (imageSpan2, 0, 5, Spannable. SPAN_EXCLUSIVE_EXCLUSIVE); 36 37 btn_big.append (spannableString1); 38 btn_big.append ("My buttons"); 39 btn_big.append (spannableString2); 40} 41}
/ButtonDemo1/res/layout/activity02.xml
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent" 5 android: orientation = "vertical"> 6 7 <LinearLayout 8 android: layout_width = "match_parent" 9 android: layout_height = "wrap_content" 10 android: orientation = "horizontal" 11> 12 13 <! -- Android: drawableTop = "@ drawable/star": displays images in text --> 14 <Button15 android: layout_width = "wrap_content" 16 android: layout_height = "wrap_content" 17 android: text = "1" 18 android: drawableTop = "@ drawable/star" 19/> 20 21 <! -- Android: drawablePadding = "30dp implements a 30dp gap between text and text images --> 22 <Button23 android: layout_width =" wrap_content "24 android: layout_height = "wrap_content" 25 android: text = "2" 26 android: drawableTop = "@ drawable/star" 27 android: drawablePadding = "30dp" 28/> 29 30 <Button31 android: layout_width = "wrap_content" 32 android: layout_height = "wrap_content" 33 android: text = "3" 34 android: drawableRight = "@ drawable/star" 35/> 36 37 <Button38 android: layout_width = "wrap_content" 39 android: layout_height = "wrap_content" 40 android: text = "4" 41 android: drawableLeft = "@ drawable/star" 42/> 43 </LinearLayout> 44 45 <Button 46 android: id = "@ + id/btn_big" 47 android: layout_width = "180dp" 48 android: layout_height = "200dp" 49 50/> 51 52 </LinearLayout>