To use Gallery in a recent project, the function of GridView must be available because of business needs.
[Java]
1. package com. easymorse. grid. demo;
2.
3. import java. util. ArrayList;
4. import java. util. HashMap;
5.
6. import android. app. ListActivity;
7. import android. OS. Bundle;
8. import android. view. LayoutInflater;
9. import android. view. View;
10. import android. widget. ArrayAdapter;
11. import android. widget. GridView;
12. import android. widget. ListView;
13. import android. widget. SimpleAdapter;
14.
15. public class GridDemoActivity extends ListActivity {
16./** Called when the activity is first created .*/
17. @ Override
18. public void onCreate (Bundle savedInstanceState ){
19. super. onCreate (savedInstanceState );
20. setContentView (R. layout. main );
21.
22. LayoutInflater layoutInflater = (LayoutInflater) this
23. getSystemService ("layout_inflater ");
24. View headerView = layoutInflater. inflate (R. layout. list_header, null );
25. setGridView (headerView );
26. ListView listView = (ListView) this. findViewById (android. R. id. list );
27. listView. addHeaderView (headerView );
28. listView. setAdapter (new ArrayAdapter <String> (this, android. r. layout. simple_list_item_1, new String [] {"", "", "});
29 .}
30.
31. private void setGridView (View view ){
32. GridView gridView = (GridView) view. findViewById (R. id. grid );
33. gridView. setNumColumns (10 );
34.
35. ArrayList <HashMap <String, Object> items = new ArrayList <HashMap <String, Object> ();
36.
37. for (int I = 0; I <10; I ++ ){
38. HashMap <String, Object> map = new HashMap <String, Object> ();
39. map. put ("ItemImage", R. drawable. k );
40. map. put ("ItemText", "" + "(" + I + ")");
41. items. add (map );
42 .}
43.
44. SimpleAdapter adapter = new SimpleAdapter (this, items, R. layout. item,
45. new String [] {"ItemImage", "ItemText"}, new int [] {
46. R. id. ItemImage, R. id. ItemText });
47. gridView. setAdapter (adapter );
48 .}
49 .}
Item. xml
[Java]
1. <? Xml version = "1.0" encoding = "UTF-8"?>
2. <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
3. android: paddingBottom = "10.0dip" android: layout_width = "90.0dip"
4. android: layout_height = "1401_dip">
5. <ImageView android: id = "@ + id/ItemImage" android: layout_width = "80366dip"
6. android: layout_height = "108.0dip" android: layout_marginLeft = "10.0dip"
7. android: layout_centerHorizontal = "true">
8. </ImageView>
9. <TextView android: layout_below = "@ + id/ItemImage" android: id = "@ + id/ItemText"
10. android: ellipsize = "end" android: layout_width = "80366dip"
11. android: layout_height = "2620.dip" android: layout_marginTop = "5.0dip"
12. android: singleLine = "true" android: layout_centerHorizontal = "true">
13. </TextView>
14. </RelativeLayout>
List_header.xml
[Html]
1. <? Xml version = "1.0" encoding = "UTF-8"?>
2. <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
3. android: orientation = "vertical" android: layout_width = "fill_parent"
4. android: layout_height = "200dp">
5. <TextView android: layout_width = "fill_parent"
6. android: layout_height = "wrap_content" android: text = "Recent visitors"/>
7. <HorizontalScrollView android: layout_width = "fill_parent"
8. android: layout_height = "160dp">
9. <FrameLayout android: layout_width = "fill_parent"
10. android: layout_height = "match_parent">
11. <LinearLayout android: layout_width = "1100dp"
12. android: layout_height = "match_parent" android: orientation = "horizontal">
13. <GridView android: id = "@ + id/grid" android: layout_width = "fill_parent"
14. android: gravity = "center" android: layout_height = "fill_parent"
15. android: horizontalSpacing = "1.0dip" android: verticalSpacing = "1.0dip"
16. android: stretchMode = "spacingWidthUniform" android: numColumns = "auto_fit"
17. android: columnWidth = "80dip">
18. </GridView>
19. </LinearLayout>
20. </FrameLayout>
21. </HorizontalScrollView>
22. </LinearLayout>
Main, xml
[Html]
1. <? Xml version = "1.0" encoding = "UTF-8"?>
2. <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
3. android: orientation = "vertical" android: layout_width = "fill_parent"
4. android: layout_height = "200dp">
5. <TextView android: layout_width = "fill_parent"
6. android: layout_height = "wrap_content" android: text = "Recent visitors"/>
7. <HorizontalScrollView android: layout_width = "fill_parent"
8. android: layout_height = "160dp">
9. <FrameLayout android: layout_width = "fill_parent"
10. android: layout_height = "match_parent">
11. <LinearLayout android: layout_width = "1100dp"
12. android: layout_height = "match_parent" android: orientation = "horizontal">
13. <GridView android: id = "@ + id/grid" android: layout_width = "fill_parent"
14. android: gravity = "center" android: layout_height = "fill_parent"
15. android: horizontalSpacing = "1.0dip" android: verticalSpacing = "1.0dip"
16. android: stretchMode = "spacingWidthUniform" android: numColumns = "auto_fit"
17. android: columnWidth = "80dip">
18. </GridView>
19. </LinearLayout>
20. </FrameLayout>
21. </HorizontalScrollView>
22. </LinearLayout>
AndroidManifest. xml
<? 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">
<TextView android: layout_width = "fill_parent"
Android: layout_height = "wrap_content" android: text = "@ string/hello"/>
<ListView android: layout_width = "fill_parent"
Android: layout_height = "fill_parent" android: id = "@ android: id/list" android: cacheColorHint = "#00000000"/>
</LinearLayout>
From sun6223508