AndroidUI control series: GridView (grid layout)

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

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.