Learn android<gallery drag components from scratch. 27 .>

Source: Internet
Author: User

The gallery component enables the gallery display of pictures. Loop a picture in the gallery

The inheritance structure is as follows

Java.lang.Object
? Android.view.View

&NBSP;&NBSP;&NBSP;? android.view.viewgroup


&NBSP;&NBSP;&NBSP;? android.widget.adapterview<t extends  Android.widget.adapter>



? Android.widget.AbsSpinner




? Android.widget.Gallery
using the gallery component also uses the adapter to configure the data

Here we use Baseadapter to configure the data

XML file


<span style= "FONT-SIZE:18PX;" ><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=". Mainactivity ">    <gallery        android:layout_margintop=" 20DP "        android:id=" @+id/gallery        " Android:layout_width= "Fill_parent"        android:layout_height= "wrap_content"         /></relativelayout> </span>

Java files


<span style= "FONT-SIZE:18PX;" >package Com.example.gallery;import android.os.bundle;import Android. R.integer;import Android.app.activity;import Android.view.menu;import Android.view.view;import Android.view.viewgroup;import Android.widget.baseadapter;import Android.widget.gallery;import Android.widget.gallery.layoutparams;import Android.widget.imageview;public class Mainactivity extends Activity { Private Gallery gallery;//declaration galleryprivate int images[] = {r.drawable.a1, r.drawable.a2, r.drawable.a3,r.drawable.a4, R . Drawable.a5, R.drawable.a6, R.drawable.a7,r.drawable.a8, r.drawable.a9, r.drawable.a10, R.DRAWABLE.A11, R.drawable.a12, R.drawable.a13, r.drawable.a14};//picture data @overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); gallery= (gallery) This.findViewById ( R.id.gallery);//Get Gallerygallery.setadapter (New Myadapter ());} New class inherits Baseadapterclass Myadapter extends baseadapter{@Overridepublic int GetCount ({//TODO auto-generated method stub//Data amount is the maximum value of the shaping data return integer.max_value;} @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 MyView, ViewGroup parent) {//TODO auto-generated method stub//New I that View object ImageView image=new ImageView (Getapplicationcontext ());//Set Picture data Image.setimageresource (images[position% Images.length]);//Set the alignment of the picture Image.setscaletype (ImageView.ScaleType.FIT_XY);//Set the size of the picture image.setlayoutparams (new Gallery.layoutparams (Layoutparams.wrap_content, layoutparams.wrap_content));//returns ImageView return image;}} </span>

Effects such as


Gallery is to load the data through adapter and then display the gallery in the UI

Next section forecast: GridView grid view



Learn android<gallery drag components from scratch. 27 .>

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.