Learning android from scratch (GridView grid view. Twenty-eight .)

Source: Internet
Author: User

Learning android from scratch (GridView grid view. Twenty-eight .)
The GridView component displays all components in a grid. For example, when creating an album, all the images are displayed in different grids of the same size, the inheritance structure of this component is as follows: java. lang. object? Android. view. View? Android. view. ViewGroup? Android. widget. AdapterView ? Android. widget. AbsListView? Common android. widget. GridView Methods

1 Public GridView (Context context) Structure

Create a GridView object
2 Public void setStretchMode (int stretchMode) Normal Android: stretchMode Scaling Mode
3 Public void setVerticalSpacing (int verticalSpacing) Normal Android: verticalSpacing Set Vertical spacing
4 Public void setHorizontalSpacing (int horizontalSpacing) Normal Android: horizontalSpacing Set Horizontal spacing
5 Public void setNumColumns (int numColumns) Normal Android: numColumns Sets the data volume displayed in each column. If it is set to auto_fit, it is automatically set.
6 Public void setSelection (int position) Normal

Set the selected items by default
7 Public void setGravity (int gravity) Normal Android: gravity Sets the alignment mode, which is specified by the Gravity class.
8 Public void setAdapter (ListAdapter adapter) Normal

Set the display image set


Of course, to use the GridView, you must use the Adapter dataset class to configure image data.
You can use SimpleAdapter and BaseAdapter to configure the adapter. Today, I use BaseAdapter to configure the Adapter data.
XML file
     
  
 

JAVA files
Package com. example. gridview; import android. app. activity; import android. app. alertDialog; import android. content. dialogInterface; import android. content. dialogInterface. onClickListener; import android. OS. bundle; import android. view. view; import android. view. viewGroup; import android. view. viewGroup. layoutParams; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. baseAdapter; import android. widget. gridView; import android. widget. imageView; public class MainActivity extends Activity {private GridView gridView; // initialize GridViewprivate int images [] = {R. drawable. a1, R. drawable. a2, R. drawable. a3, R. drawable. a4, R. drawable. a5, R. drawable. a6, R. drawable. a7, R. drawable. a8, R. drawable. a9, R. drawable. a10, R. drawable. a11, R. drawable. a12, R. drawable. a13, R. drawable. a14, R. drawable. a15, R. drawable. a16}; // image data @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // obtain the GridView object gridView = (GridView) this. findViewById (R. id. gridview); // set AdptergridView. setAdapter (new MyAdapter (); // sets the listener event gridView. setOnItemClickListener (new OnItemClickListener () {@ Overridepublic void onItemClick (AdapterView
 AdapterView, View view, int position, long id) {// TODO Auto-generated method stub // set the view space ImageView image = new ImageView (MainActivity. this); // sets the image information. setImageResource (images [position]); // you can specify the image size alignment mode. setScaleType (ImageView. scaleType. CENTER); // set the image size. setLayoutParams (new LayoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT); // create dialog box AlertDialog. builder builder = new AlertDialog. builder (MainActivity. this ). setTitle ("view image "). setView (image ). setIcon (android. r. drawable. btn_star_big_on ). setNegativeButton ("off", new OnClickListener () {@ Overridepublic void onClick (DialogInterface arg0, int arg1) {// TODO Auto-generated method stub }}); // create a dialog box builder. create (); // display the dialog box builder. show () ;}});} class MyAdapter extends BaseAdapter {@ Overridepublic int getCount () {// TODO Auto-generated method stubreturn images. length ;}@ Overridepublic Object getItem (int position) {// TODO Auto-generated method stubreturn position ;}@ Overridepublic long getItemId (int position) {// TODO Auto-generated method stubreturn position;} @ Overridepublic View getView (int position, View myView, ViewGroup parent) {// TODO Auto-generated method stub // return imageView object ImageView imageView = new ImageView (MainActivity. this); imageView. setImageResource (images [position]); imageView. setScaleType (ImageView. scaleType. CENTER); return imageView ;}}}

Effect
You can use the GridView component to achieve a colorful menu effect.

Next prediction: TabHost tag component

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.