Use of Android Gallery

Source: Internet
Author: User

1:

XML

<Linearlayout 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">

<Gallery
Android: Id = "@ + ID/imagegallery"
Android: layout_width = "fill_parent"
Android: layout_height = "match_parent"/>

</Linearlayout>

2:

Java files

Package com. example. Cloud. hdplayer2.faq;

Import Android. App. activity;
Import Android. content. context;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. viewgroup;
Import Android. View. viewgroup. layoutparams;
Import Android. widget. baseadapter;
Import Android. widget. Gallery;
Import Android. widget. imageview;

Public class imageshowactivity extends activity {
Private gallery imagegallery;
Private imageview image;
Private string Pos;
Private imageadapter;
Private int [] images = new int [] {R. drawable. M1, R. drawable. m2,
R. drawable. m3, R. drawable. M1, R. drawable. M1, R. drawable. M1,
R. drawable. M1 };

Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. activity_imageshow );


Imagegallery = (Gallery) findviewbyid (R. Id. imagegallery );
Imageadapter = new imageadapter (getapplicationcontext ());
Initview ();
}

Private void initview (){
Imagegallery. setadapter (imageadapter );
Imagegallery. setselection (0 );

}

Public class imageadapter extends baseadapter {
Private context mcontext;

Public imageadapter (context ){
Mcontext = context;
}

Public int getcount (){
Return images. length;
}

Public object getitem (INT position ){
Return position;
}

Public long getitemid (INT position ){
Return position;
}

Public View getview (INT position, view convertview, viewgroup parent ){
Imageview image = new imageview (mcontext );
Image. setimageresource (images [position]);
Image. setadjustviewbounds (true );
Image. setlayoutparams (new gallery. layoutparams (
Layoutparams. match_parent, layoutparams. match_parent ));
Return image;
}
}

}

Related Article

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.