Gallery is the image library control in Android. Let's take a look at the effect first.
Source code download
I. Introduction
Locked in the center to display items in the list horizontally.
Ii. Instances
1. layout File
xmlns: Android =" http://schemas.android.com/apk/res/android "
Android: layout_width = "fill_parent"
Android: orientation = "vertical"
Android: layout_height = "wrap_content">
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
/>
Android: orientation = "vertical"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content">
<Gallery Android: Id = "@ + ID/gallery1"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: gravity = "center_vertical"
Android: spacing = "16dp"
/>
</Linearlayout>
</Linearlayout>
2. Property File
<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<Declare-styleable name = "toggleprefattrs">
<ATTR name = "Android: preferencelayoutchild"/>
</Declare-styleable>
<! -- These are the attributes that we want to retrieve from the theme
In View/gallery1.java -->
<Declare-styleable name = "gallery1">
<ATTR name = "Android: galleryitembackground"/>
</Declare-styleable>
<Declare-styleable name = "labelview">
<ATTR name = "text" format = "string"/>
<ATTR name = "textcolor" format = "color"/>
<ATTR name = "textsize" format = "dimension"/>
</Declare-styleable>
</Resources>
3.Code
/**
*
*/
Package wjq. widgetdemo;
Import Android. R. layout;
Import Android. App. activity;
Import Android. content. context;
Import Android. content. res. typedarray;
Import Android. database. cursor;
Import Android. OS. Bundle;
Import Android. provider. Contacts. People;
Import Android. View. contextmenu;
Import Android. View. menuitem;
Import Android. View. view;
Import Android. View. viewgroup;
Import Android. View. contextmenu. contextmenuinfo;
Import Android. widget. baseadapter;
Import Android. widget. Gallery;
Import Android. widget. imageview;
Import Android. widget. simplecursoradapter;
Import Android. widget. spinneradapter;
Import Android. widget. Toast;
Import Android. widget. adapterview. adaptercontextmenuinfo;
/**
* @ Author: The Eternal memory
*
*/
Public class gallerydemo extends activity {
Private gallery Gallery;
Private gallery gallery1;
/*
* (Non-javadoc)
*
* @ See Android. App. Activity # oncreate (Android. OS. Bundle)
*/
@ Override
Protected void oncreate (bundle savedinstancestate ){
// Todo auto-generated method stub
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. gallerypage );
Gallery = (Gallery) findviewbyid (R. Id. Gallery );
Gallery. setadapter (New imageadapter (this ));
Registerforcontextmenu (Gallery );
Cursor c = getcontentresolver (). Query (people. content_uri, null );
Startmanagingcursor (C );
Spinneradapter adapter = new simplecursoradapter (this,
// Use a template that displays a Text View
Android. R. layout. simple_gallery_item,
// Give the cursor to the list adatper
C,
// Map the name column in The People database...
New String [] {People. name },
// The "text1" view defined in the XML template
New int [] {Android. R. Id. text1 });
Gallery1 = (Gallery) findviewbyid (R. Id. gallery1 );
Gallery1.setadapter (adapter );
}
@ Override
Public void oncreatecontextmenu (contextmenu menu, view V,
Contextmenuinfo menuinfo ){
Menu. Add ("action ");
}
@ override
Public Boolean oncontextitemselected (menuitem item) {
adaptercontextmenuinfo = (adaptercontextmenuinfo) item
. getmenuinfo ();
toast. maketext (this, "longpress:" + info. position, toast. length_short)
. show ();
return true;
}
public class imageadapter extends baseadapter {
int mgalleryitembackground;
private context mcontext;
Private integer [] mimageids = {R. drawable. B, R. drawable. C,
R. drawable. D, R. drawable. F, R. drawable. g };
Public imageadapter (context ){
Mcontext = context;
Typedarray A = obtainstyledattributes (R. styleable. gallery1 );
Mgalleryitembackground = A. getresourceid (
R. styleable. gallery1_android_galleryitembackground, 0 );
A. Recycle ();
}
@ Override
Public int getcount (){
Return mimageids. length;
}
@ Override
Public object getitem (INT position ){
Return position;
}
@ Override
Public long getitemid (INT position ){
Return position;
}
@ Override
Public View getview (INT position, view convertview, viewgroup parent ){
Imageview I = new imageview (mcontext );
I. setimageresource (mimageids [position]);
I. setscaletype (imageview. scaletype. fit_xy );
I. setlayoutparams (new gallery. layoutparams (300,400 ));
// The preferred gallery item background
I. setbackgroundresource (mgalleryitembackground );
Return I;
}
}
}