Android UI Control Series: GridView (Grid layout)

Source: Internet
Author: User
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)!

  • 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.