In Android, how does one place an image in the text

Source: Internet
Author: User
Are you talking about this?
If so, let's look at the source code:
Gridview. xml Xml Code
  1. <? XmlVersion = "1.0" encoding = "UTF-8"?>
  2. <GridViewXmlns: android = "http://schemas.android.com/apk/res/android"
  3. Android: id = "@ + id/gridview"
  4. Android: layout_width = "fill_parent"
  5. Android: layout_height = "fill_parent"
  6. Android: numColumns = "auto_fit"
  7. Android: verticalSpacing = "10dp"
  8. Android: horizontalSpacing = "10dp"
  9. Android: columnWidth = "90dp"
  10. Android: stretchMode = "columnWidth"
  11. Android: gravity = "center"
  12. />
<? Xml version = "1.0" encoding = "UTF-8"?> <GridView xmlns: android = "http://schemas.android.com/apk/res/android" android: id = "@ + id/gridview" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: numColumns = "auto_fit" android: verticalSpacing = "10dp" android: horizontalSpacing = "10dp" android: columnWidth = "90dp" android: stretchMode = "columnWidth" android: gravity = "center"/>

Item. xml Xml Code

  1. <? XmlVersion = "1.0" encoding = "UTF-8"?>
  2. <RelativeLayoutXmlns: android = "http://schemas.android.com/apk/res/android"
  3. Android: layout_height = "wrap_content"
  4. Android: layout_width = "fill_parent"
  5. Android: paddingBottom = "4dip"
  6. >
  7. <ImageView
  8. Android: id = "@ + id/ItemImage"
  9. Android: layout_height = "wrap_content"
  10. Android: layout_width = "wrap_content"
  11. Android: layout_centerHorizontal = "true"
  12. />
  13. <TextView
  14. Android: id = "@ + id/ItemText"
  15. Android: layout_width = "wrap_content"
  16. Android: layout_below = "@ + id/ItemImage"
  17. Android: layout_height = "wrap_content"
  18. Android: layout_centerHorizontal = "true"
  19. Android: text = "TextView01"
  20. />
  21. </RelativeLayout>
<? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_height = "wrap_content" android: layout_width = "fill_parent" android: paddingBottom = "4dip"> <ImageViewandroid: id = "@ + id/ItemImage" android: layout_height = "wrap_content" android: layout_width = "wrap_content" android: layout_centerHorizontal = "true"/> <TextViewandroid: id = "@ + id/ItemText" android: layout_width = "wrap_content" android: layout_below = "@ + id/ItemImage" android: layout_height = "wrap_content" android: layout_centerHorizontal = "true" android: text = "TextView01"/> </RelativeLayout>

Activity Java code

  1. Package com. test;
  2. Import java. util. ArrayList;
  3. Import java. util. HashMap;
  4. Import android. app. Activity;
  5. Import android. OS. Bundle;
  6. Import android. view. View;
  7. Import android. widget. AdapterView;
  8. Import android. widget. AdapterView. OnItemClickListener;
  9. Import android. widget. GridView;
  10. Import android. widget. SimpleAdapter;
  11. Public class TestGridView extends Activity {
  12. Private GridView;
  13. Public void onCreate (Bundle savedInstanceState ){
  14. Super. onCreate (savedInstanceState );
  15. SetContentView (R. layout. gridview );
  16. Gridview = (GridView) findViewById (R. id. gridview );
  17. // Generate dynamic array and transfer data
  18. ArrayList <HashMap <String, Object> lstImageItem = new ArrayList <HashMap <String, Object> ();
  19. For (int I = 0; I <10; I ++ ){
  20. HashMap <String, Object> map = new HashMap <String, Object> ();
  21. Map. put ("ItemImage", R. drawable. icon); // Add the ID of the image resource.
  22. Map. put ("ItemText", "NO." + String. valueOf (I); // do ItemText by serial number.
  23. LstImageItem. add (map );
  24. }
  25. // Generate the ImageItem of the adapter <====> dynamic array elements, one-to-one correspondence between the two
  26. SimpleAdapter saImageItems = new SimpleAdapter (this, // No explanation
  27. LstImageItem, // Data Source
  28. R. layout. item, // XML Implementation of night_item
  29. // Subitem of the dynamic array and ImageItem
  30. New String [] {"ItemImage", "ItemText "},
  31. // An ImageView in the XML file of ImageItem, two TextView IDS
  32. New int [] {R. id. ItemImage, R. id. ItemText });
  33. // Add and display
  34. Gridview. setAdapter (saImageItems );
  35. // Add Message Processing
  36. Gridview. setOnItemClickListener (new ItemClickListener ());
  37. }
  38. // When AdapterView is clicked (touch screen or keyboard), the returned Item Click Event
  39. Class ItemClickListener implements OnItemClickListener {
  40. Public void onItemClick (AdapterView <?> Arg0, // The AdapterView where the click happened
  41. View arg1, // The view within the AdapterView that was clicked
  42. Int arg2, // The position of the view in the adapter
  43. Long arg3 // The row id of the item that was clicked
  44. ){
  45. // In this example, arg2 = arg3
  46. @ SuppressWarnings ("unchecked ")
  47. HashMap <String, Object> item = (HashMap <String, Object>) arg0.getItemAtPosition (arg2 );
  48. // Display the ItemText of the selected Item
  49. SetTitle (String) item. get ("ItemText "));
  50. }
  51. }
  52. }
Package com. test; import java. util. arrayList; import java. util. hashMap; import android. app. activity; import android. OS. bundle; import android. view. view; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. gridView; import android. widget. simpleAdapter; public class TestGridView extends Activity {private GridView gridview; public void onCreate (Bundle s AvedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. gridview); gridview = (GridView) findViewById (R. id. gridview); // generates a dynamic array and transfers data to ArrayList <HashMap <String, Object> lstImageItem = new ArrayList <HashMap <String, Object> (); for (int I = 0; I <10; I ++) {HashMap <String, Object> map = new HashMap <String, Object> (); map. put ("ItemImage", R. drawable. icon); // Add the IDmap of image resources. put ("ItemT Ext "," NO. "+ String. valueOf (I); // ItemTextlstImageItem by serial number. add (map) ;}// generate the ImageItem of the adapter <===> dynamic array elements. The two correspond to SimpleAdapter saImageItems = new SimpleAdapter (this, // There is no explanation for lstImageItem, // data source R. layout. XML implementation of item, // night_item // the subitem of the dynamic array and ImageItem new String [] {"ItemImage", "ItemText "}, // an ImageView in the XML file of ImageItem, two TextView IDnew int [] {R. id. itemImage, R. id. itemText}); // Add and display the gridview. set Adapter (saImageItems); // Add a message to process the gridview. setOnItemClickListener (new ItemClickListener ();} // when AdapterView is clicked (touch screen or keyboard), the returned Item clicks the event class ItemClickListener implements OnItemClickListener {public void onItemClick (AdapterView <?> Arg0, // The AdapterView where the click happenedView arg1, // The view within the AdapterView that was clickedint arg2, // The position of the view in the adapterlong arg3 // The row id of the item that was clicked) {// in this example, arg2 = arg3 @ SuppressWarnings ("unchecked ") hashMap <String, Object> item = (HashMap <String, Object>) arg0.getItemAtPosition (arg2); // display ItemTextsetTitle (String) Item of the selected item. get ("ItemText "));}}}

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.