Android Gallery and GridView components:
Gallery Gallery
Gallery is an internal element that scrolls horizontally and can position the currently selected child element in its center layout component.
Let's take a look at the operation of the example directly.
The following code, the relevant explanations are put in the code.
1. Establish a new project Hellogallery
2, copy wallpaper_0.jpg...wallpaper_9.jpg 10 picture files to res/drawable directory
3, the contents of the Res/layout/main.xml document are as follows:
<?xml version= "1.0" encoding= "Utf-8"?> <framelayout android:layout_height=
"Fill_parent" Android: Layout_width= "Fill_parent" xmlns:android= "http://schemas.android.com/apk/res/android" android:id= "@+id/" FrameLayout01 ">
<imageview android:layout_height=" fill_parent "android:layout_width=" Fill_parent " Android:id= "@+id/imageview01" android:src= "@drawable/wallpaper_0" >
</imageview>
<gallery android:layout_height= "Wrap_content" android:layout_width= "fill_parent" xmlns:android= "http://" Schemas.android.com/apk/res/android "android:id=" @+id/gallery01 "android:spacing=" 5DP ">
</gallery>
</framelayout>
In which we use framelayout to achieve overlay effect, using ImageView to display large graphs, gallery to show galleries, android:spacing= "5DP" property is used to set the interval between the elements.
4. The new attrs.xml content in the res/values/directory is as follows:
<?xml version= "1.0" encoding= "UTF-8"?>
<resources>
<declare-styleable= "Name=" Hellogallery ">
<attr name=" Android:galleryitembackground ">
</attr></declare>
</resources>
5. The contents of the Mainhellogallery.java are as follows:
Package android.basic.lesson13;
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.widget.Toast; The public class Mainhellogallery extends activity {/** called the ' when ' is the ' The activity ' is the ' the './@Override Public
void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Define UI Components Final ImageView iv= (ImageView) Findviewbyid (R.ID.IMAGEVIEW01);
Gallery g = (Gallery) Findviewbyid (R.ID.GALLERY01);
Sets the picture-matching G.setadapter (new Imageadapter (this));
Set Adapterview click on the Listener, Gallery is Adapterview subclass G.setonitemclicklistener (New Onitemclicklistener () {@Override public void Onitemclick (adapterview<?> parent, view view, int position, long ID) {//show click on the first few pictures
Toast.maketext (Mainhellogallery.this, "" + Position, Toast.length_long). Show ();
Set the ImageView of the background section to display a picture of the current item Iv.setimageresource ((ImageView) view). GetId ());
}
}); }//Defines the matching of the inherited Baseadapter public class Imageadapter extends Baseadapter {//item decorated background int Mgalleryitembackgrou
nd
Contextual object private context Mcontext;
Picture array private integer[] Mimageids = {r.drawable.wallpaper_0, r.drawable.wallpaper_1, r.drawable.wallpaper_2, R.drawable.wallpaper_3, R.drawable.wallpaper_4, R.drawable.wallpaper_5, R.drawable.wallpaper_6, R
. drawable.wallpaper_7, R.drawable.wallpaper_8, r.drawable.wallpaper_9};
Construction method Public Imageadapter (context c) {mcontext = C;
Read Styleable resource TypedArray a = Obtainstyledattributes (r.styleable.hellogallery); MgalLeryitembackground = A.getresourceid (r.styleable.hellogallery_android_galleryitembackground, 0);
A.recycle ();
//Returns the number of items @Override public int GetCount () {return mimageids.length;
//Returns the project @Override public Object getitem (int position) {return position;
//Returns the project ID @Override public long getitemid (int position) {return position; ///Return view @Override public view GetView (int position, view Convertview, ViewGroup parent) {ImageView I
v = new ImageView (mcontext);
Iv.setimageresource (Mimageids[position]);
Sets the ID for the generated imageview, the ID is -1 iv.setid (mimageids[position) without the setting;
Iv.setlayoutparams (New Gallery.layoutparams (120, 160));
Iv.setscaletype (ImageView.ScaleType.FIT_XY);
Iv.setbackgroundresource (Mgalleryitembackground);
return IV; }
}
}
When we click on a picture, the picture of the child element is displayed in the ImageView component at the back level. Interested students can learn about Adapterview's inheritance relationship:
The above is the introduction of the Android Gallery and GridView components, follow-up to the relevant knowledge to supplement, thank you for your support for this site!