Controls related to Adapter in android ---- GridView and androidadapter controls
GridView (grid view)
1. The GridView (grid view) has many controls, which are the same as listView in many places. However, the GridView can display multiple columns, while the listView can only display one column, I personally think this is the biggest difference. Common attributes:
Android: columnWidth: Set the column width
Android: gravity: Alignment of components
Android: horizontalSpacing: The spacing of each cell in the horizontal direction
Android: verticalSpacing: spacing of each cell in the vertical direction
Android: numColumns: set the number of columns. The default value is 1.
Android: stretchMode: sets the stretch mode. Optional values: none. spacingWidth: just stretch the table element's own spacingWidthUniform to stretch both the element spacing and the gap between them
Ii. Use instances
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <GridView android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="2" android:id="@+id/gv"> </GridView></LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "horizontal"> <ImageView android: id = "@ + id/image1" android: layout_width = "100dp" android: layout_height = "100dp" android: src = "@ mipmap/ic_launcher"/> <TextView android: id = "@ + id/text1" android: layout_width = "match_parent" android: layout_height = "100dp" android: gravity = "center" android: text = "Haha" android: textSize = "30sp"/> </LinearLayout>
Java files
Package com. example. test3; import android. app. activity; import android. OS. bundle; import android. widget. gridView; import android. widget. simpleAdapter; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; public class MainActivity extends Activity {private GridView; private List <Map <String, Object> list; private SimpleAdapter adapter; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); gridView = (GridView) findViewById (R. id. gv); list = new ArrayList <> (); for (int I = 0; I <10; I ++) {Map <String, object> map = new HashMap <> (); map. put ("image", R. mipmap. ic_launcher); map. put ("content", "Haha" + I); list. add (map);} adapter = new SimpleAdapter (MainActivity. this, list, R. layout. item, new String [] {"image", "content"}, new int [] {R. id. image1, R. id. text1}); gridView. setAdapter (adapter );}}