Learning notes for Android: Gallery (2)

Source: Internet
Author: User

Package xiaosi. gallery; import android. app. activity; import android. content. context; import android. content. res. typedarray; 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. gallery; import android. widget. imageview; import android. wi DGET. toast; public class galleryactivity extends activity {/** called when the activity is first created. */private gallery = NULL; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); gallery = (Gallery) findviewbyid (R. id. gallery); // sets the image adapter Gallery. setadapter (New imageadapter (this); Gallery. setspacing (5); // sets the listener g Allery. setonitemclicklistener (New onitemclicklistener () {public void onitemclick (adapterview <?> Arg0, view arg1, int arg2, long arg3) {toast. maketext (galleryactivity. this, "click the" + arg2 + "image", toast. length_long ). show () ;}}) ;}} class imageadapter extends baseadapter {int mgalleryitembackground; private context; // image source array private integer [] imageinteger = {R. drawable. a, R. drawable. b, R. drawable. c, R. drawable. d}; Public imageadapter (context c) {context = C; typedarray ATTR = context. obtainstyledattributes (R. styleable. hellogallery); mgalleryitembackground = ATTR. getresourceid (R. styleable. hellogallery_android_galleryitembackground, 0); ATTR. recycle ();} // obtain the number of images public int getcount () {return imageinteger. length ;}// obtain the position of the image in the library public object getitem (INT position) {return position ;}// obtain the image ID public long getitemid (INT position) {return position ;} public View getview (INT position, view convertview, viewgroup parent) {imageview = new imageview (context); // set the imageview resource for imageview. setimageresource (imageinteger [position]); // sets the display ratio type imageview. setscaletype (imageview. scaletype. fit_xy); // set the layout image 120*80 imageview. setlayoutparams (new gallery. layoutparams (180,100); imageview. setbackgroundresource (mgalleryitembackground); Return imageview ;}}

Mian. xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" ><Gallery    android:id="@+id/gallery"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:gravity="bottom"        android:background="?android:galleryItemBackground"/></LinearLayout>

Create a New XML file inRes/values/directoryName attrs. xml.

<?xml version="1.0" encoding="utf-8"?><resources>        <declare-styleable name="HelloGallery">                <attr name="android:galleryItemBackground" />        </declare-styleable></resources>

This is a customized styleable resource that can be applied to a layout.

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.