The GridView displays content in a row and column, generally suitable for displaying icons, pictures, and so on, primarily for setting adapter
Here is mainly the basic Baseadapter class, overriding the method in which the main is to override the GetView method to set the display format of the picture
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 th E activity is first created. */Private GridView GV; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); The GridView Object gv= (GridView) Findviewbyid (R.ID.GRIDVIEW01) is obtained by Findviewbyid method; Sets the number of rows in the GridView Gv.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.im G03, R.drawable.img04, R.DRAWABLE.IMG05, r.drawable.img0 6, 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}; Contextual object context context; Construction Method Myadapter (context context) {This.context=context; }//Get quantity public int GetCount () {return imgs.length; }//Gets the current option public Object getItem (int item) {return item; } Gets the current option ID public long getitemid (int id) {return id; }//Create View method public view GetView (int position,view convertview,viewgroup parent) { ImageView ImageView; if (convertview==null) {//Instantiate ImageView object Imagevie W=new ImageView (context); Set ImageView object Layout Imageview.setlayoutparams (new Gridview.layoutparams (45,45)); Sets the boundary alignment Imageview.setadjustviewbounds (false); Set the scale type Imageview.setscaletype (ImageView.ScaleType.CENTER_CROP); Set spacing imageview.setpadding (8,8,8,8); }else {imageview= (imageView) Convertview; }//Set picture resources for ImageView Imageview.setimageresource (Imgs[position]); return imageView; } }}
Main.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" > <gridview android:id= "@+id/gridview01" android:layout_width= "wrap _content " android:layout_height=" wrap_content "></GridView> </LinearLayout>
The operation results are as follows
The above is the Android UI Control series: GridView (Grid layout) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!