The GridView displays content by row and column. it is generally suitable for displaying icons, images, and other content. it is mainly used to set the Adapter GridView to display content by row and column. it is generally suitable for displaying icons, images, and other content, it is mainly used to set the Adapter
Here, we mainly use the basic BaseAdapter class to override the methods in the class, and mainly rewrite the getView method to set the image display format.
The instance code is as follows:
GridViewTest. java
/** @ Author hualang */package org. hualang. grid; import android. app. activity; import android. content. context; import android. OS. bundle; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. gridView; import android. widget. imageView; public class GridViewTest extends Activity {/** Called when the activity is first created. */private GridView gv; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // Obtain the GridView object gv = (GridView) findViewById (R. id. gridView01); // you can specify the number of rows in the GridView. setNumColumns (4); gv. setAdapter (new MyAdapter (this);} // custom adapter class MyAdapter extends BaseAdapter {// Image id array private Integer [] imgs = {R. drawable. img01, R. drawable. img02, R. drawable. img03, R. drawable. img04, R. drawable. img05, R. drawable. img06, R. drawable. img07, R. drawable. img08, R. drawable. img01, R. drawable. img02, R. drawable. img03, R. drawable. img04, R. drawable. img05, R. drawable. img06, R. drawable. img07, R. drawable. img08}; // Context object context; // Constructor MyAdapter (Context context) {this. context = context;} // Obtain the number of public int getCount () {return imgs. length;} // Obtain the current option public Object getItem (int item) {return item;} // Obtain the current option id public long getItemId (int id) {return id ;} // Create The View method public View getView (int position, View convertView, ViewGroup parent) {ImageView imageView; if (convertView = null) {// instantiate the ImageView object imageView = new ImageView (context); // Set the ImageView object layout imageView. setLayoutParams (new GridView. layoutParams (45, 45); // sets the boundary to align the imageView. setAdjustViewBounds (false); // sets the scale type imageView. setScaleType (ImageView. scaleType. CENTER_CROP); // sets the spacing of imageView. setPadding (8, 8, 8);} else {imageView = (ImageView) convertView;} // set the image resource ImageView for imageView. setImageResource (imgs [position]); return imageView ;}}}
Main. xml
The running result is as follows:
The above is the content of the Android UI control series: GridView (grid layout). For more information, see PHP Chinese network (www.php1.cn )!