The use of the GridView component of the Android series tutorial

Source: Internet
Author: User

The GridView is a network layout view that allows you to display your elements in a grid. Our desktop is a GridView.

1, create a new gridview project, activity_main.xml is as follows:

<?xml version= "1.0"  encoding= "Utf-8"?><gridview   xmlns:android= "Http://schemas.android.com/apk/res/android"   android:id= "@+id/gridview"    android:layout_width= "Fill_parent"   android:layout_height= "Fill_parent"   android: Columnwidth= "90DP"   android:numcolumns= "Auto_fit"   android:horizontalspacing= "10DP"    android:verticalspacing= "10DP"   android:stretchmode= "ColumnWidth"   android:gravity= " Center "></GRIDVIEW> 

2, modify mainactivity.java code is as follows:

Package com.example.gridview;import android.app.activity;import android.content.context;import  android.os.Bundle;import android.view.View;import android.view.ViewGroup;import  android.widget.adapterview;import android.widget.adapterview.onitemclicklistener;import  Android.widget.baseadapter;import android.widget.gridview;import android.widget.imageview;import  android.widget.toast;public class mainactivity extends activity {//Show Pictures Private &NBSP;INTEGER[]&NBSP;MTHUMBIDS&NBSP;=&NBSP;{R.DRAWABLE.A,R.DRAWABLE.B,R.DRAWABLE.C,R.DRAWABLE.D,R.DRAWABLE.E, R.drawable.ic_launcher,r.drawable.music,r.drawable.video,r.drawable.photo}; @Overrideprotected  void  OnCreate (bundle savedinstancestate)  {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); gridview gridview =  (GridView)  findviewbyid (R.id.gridview); Gridview.setadapter (new  Imageadapter (this));//Click GridviEW element Response Gridview.setonitemclicklistener (New onitemclicklistener ()  {@Overridepublic  void  Onitemclick (Adapterview<?> parent, view view, int position,long id)  { The location of the GridView element that pops up clicked Toast.maketext (Mainactivity.this, mthumbids[position], toast.length_long). Show () ;}});} Private class imageadapter extends baseadapter{private context mcontext;public  imageadapter (Context context)  {this.mcontext = context;} @Overridepublic  int getcount ()  {return mthumbids.length;} @Overridepublic  object getitem (int position)  {return mthumbids[position];} @Overridepublic  long getitemid (int arg0)  {// TODO  Auto-generated method stub return 0;} @Overridepublic  view getview (int position, view convertview, viewgroup  Parent)  {//defines a ImageView that is displayed in the GridView  imageview imageview;if (convertview==null) {Imageview = new imageview (mcontext); Imageview.setlayoutparams (new  Gridview.layoutparams (85,85)); Imageview.setscaletype (ImageView.ScaleType.CENTER_CROP); Imageview.setpadding (8, &NBSP;8,&NBSP;8,&NBSP;8);} else{imageview =  (ImageView)  convertview;} Imageview.setimageresource (Mthumbids[position]); return imageview;}}

Here we also use the custom adapter, show some pictures, and then click on an image will show the location of the picture.

3. Run the program as follows:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/75/78/wKiom1Y5v7fiiRdMAAGl10-5UfU520.jpg "title=" Qq20151104161101.jpg "alt=" Wkiom1y5v7fiirdmaagl10-5ufu520.jpg "/>

This article is from the "No Water Fish" blog, please be sure to keep this source http://javaqun.blog.51cto.com/10687700/1709670

The use of the GridView component of the Android series tutorial

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.