The GridView and gallery of Android

Source: Internet
Author: User

Gridview:

Activity_main.xml

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"      xmlns:tools= "Http://schemas.android.com/tools"     android:layout_width= "Match_parent"     android:layout_height= "Match_parent"     android:paddingbottom= "@ Dimen/activity_vertical_margin "    android:paddingleft=" @dimen/activity_horizontal_margin "     android:paddingright= "@dimen/activity_horizontal_margin"      android:paddingtop= "@dimen/activity_vertical_margin"     tools:context= " Com.example.gridview.MainActivity " >    <GridView          android:id= "@+id/gridview1_1"         android: Layout_width= "Match_parent"         android:layout_height= "wrap_content "        android:numcolumns= "Auto_fit"         android:columnwidth= "90DP"         android:verticalspacing= "2DP"          android:horizontalspacing= "2DP"         android: Stretchmode= "ColumnWidth"         android:gravity= "center"          ></GridView></RelativeLayout>

mainactivity

Package com.example.gridview;import android.app.activity;import android.content.context;import  android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import  android.view.view;import android.view.viewgroup;import android.widget.baseadapter;import  Android.widget.gridview;import android.widget.imageview;public class mainactivity extends  Activity {private GridView gridView; @Overrideprotected  void oncreate (Bundle  savedinstancestate)  {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); gridview= (GridView)  findviewbyid (r.id.gridview1_1); Myimageadapter myimageadapter=new myimageadapter (this); Gridview.setadapter (MyImageAdapter);} @Overridepublic  boolean oncreateoptionsmenu (Menu menu)  {// inflate the menu ;  this adds items to the action bar if it is&nbsP;present.getmenuinflater (). Inflate (r.menu.main, menu); return true;} @Overridepublic  boolean onoptionsitemselected (Menuitem item)  {// handle action  bar item clicks here. The action bar will// automatically  handle clicks on the home/up button, so long// as you  specify a parent activity in androidmanifest.xml.int id =  Item.getitemid ();if  (id == r.id.action_settings)  {return true;} return super.onoptionsitemselected (item);} static class myimageadapter extends baseadapter{private context ct;// To display the picture resource Private int [] images={r.drawable.th_seismometer_1,r.drawable.th_skippylite, R.drawable.th_sms_hey_blue,r.drawable.th_ssh,r.drawable.th_things1,r.drawable.th_thisday,r.drawable.th_ Seismometer_1,r.drawable.th_skippylite,r.drawable.th_sms_hey_blue,r. Drawable.th_ssh,r.drawable.th_things1,r.drawable.th_thisday}; Myimageadapter (context ct) {this.ct=ct;} @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 convertview, viewgroup  Parent)  {// todo auto-generated method stubimageview imageview;if (convertView== NULL) {Imageview=new imageview (CT);//Set the width and height of the picture Imageview.setlayoutparams (New gridview.layoutparams (85,  85));//set stretch or intercept mode Imageview.setscaletype (ImageView.ScaleType.CENTER_CROP); Imageview.setpadding (8, 8, &NBSP;8,&NBSP;8);} else{imageview= (ImageView) conveRtview;} Imageview.setimageresource (Images[position]); return imageview;}}

Gallery:

Activity_main.xml

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"      xmlns:tools= "Http://schemas.android.com/tools"     android:layout_width= "Match_parent"     android:layout_height= "Match_parent"     android:paddingbottom= "@ Dimen/activity_vertical_margin "    android:paddingleft=" @dimen/activity_horizontal_margin "     android:paddingright= "@dimen/activity_horizontal_margin"      android:paddingtop= "@dimen/activity_vertical_margin"     tools:context= " Com.example.gallery.MainActivity " >       <Gallery         android:id= "@+id/gallery1_1"          android:layout_width= "Match_parent"         android:layout_height = "Wrap_content"         android:layout_marginleft= "60DP"         android:layout_ margintop= "60DP"        >             </Gallery></RelativeLayout>

mainactivity

package com.example.gallery;import android.app.activity;import android.os.bundle;import  android.view.menu;import android.view.menuitem;import android.view.view;import  android.view.viewgroup;import android.widget.baseadapter;import android.widget.gallery;import  android.widget.imageview;public class mainactivity extends activity {private  gallery gallery; @Overrideprotected  void oncreate (bundle savedinstancestate)  { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); gallery= (gallery)  findViewById ( R.id.gallery1_1); Mygalleryadapter mygalleryadapter=new mygalleryadapter (); Gallery.setadapter (MyGalleryAdapter);} @Overridepublic  boolean oncreateoptionsmenu (Menu menu)  {// inflate the menu ; this adds items to the action bar if it is  Present.getmenuinflater (). Inflate (r.meNu.main, menu); return true;} @Overridepublic  boolean onoptionsitemselected (Menuitem item)  {// handle action  bar item clicks here. The action bar will// automatically  handle clicks on the home/up button, so long// as you  specify a parent activity in androidmanifest.xml.int id =  Item.getitemid ();if  (id == r.id.action_settings)  {return true;} return super.onoptionsitemselected (item);} Class mygalleryadapter extends baseadapter{private int [] images={r.drawable.th_ SEISMOMETER_1,R.DRAWABLE.TH_SKIPPYLITE,R.DRAWABLE.TH_SMS_HEY_BLUE,R.DRAWABLE.TH_SSH,R.DRAWABLE.TH_THINGS1, R.drawable.th_thisday,r.drawable.th_seismometer_1,r.drawable.th_skippylite,r.drawable.th_sms_hey_blue, R.drawable.th_ssh,r.drawable.th_things1,r.drawable.th_thisday}; @Overridepublic &NBSP;INT&NBSp;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 convertview, viewgroup  Parent)  {// todo auto-generated method stubimageview imageview;if (convertView== NULL) {Imageview=new imageview (mainactivity.this);} else{imageview= (ImageView) Convertview;} Imageview.setimageresource (Images[position]); return imageview;}}

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/8B/8E/wKiom1hQ7uLC1OHjAAFiUMGNV4k443.png-wh_500x0-wm_3 -wmp_4-s_2418319840.png "style=" Float:none; "title=" _fse8o] ' k~yu77m4{c ') ntg.png "alt=" Wkiom1hq7ulc1ohjaafiumgnv4k443.png-wh_50 "/>

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M02/8B/8E/wKiom1hQ7uLTRWZ9AAGMIxDxHgU103.png-wh_500x0-wm_3 -wmp_4-s_1699558918.png "style=" Float:none; "title="}s2$x2nm6s~_{1lf5g7g_sf.png "alt=" Wkiom1hq7ultrwz9aagmixdxhgu103.png-wh_50 "/>


This article is from the "matengbing" blog, make sure to keep this source http://matengbing.blog.51cto.com/11395502/1882650

The GridView and gallery of Android

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.