【Android】Chapter 5 (8) Picture Gallery (Galery)
Category: C#, Android, VS2015; Creation date: 2016-02-07 1. Introduction The gallery (also called gallery) is a layout widget used to display each picture in a horizontally scrollable list. Currently The selected picture will be centered in the view. Note: Android has deprecated this widget. The reason for deprecation is that the efficiency of using Galery is relatively low,
Category: C#, Android, VS2015;
Creation date: 2016-02-07
1. Introduction
The Gallery (also called Gallery) is a layout widget that displays each 【Android】Chapter 5 (8) Picture Gallery (Galery) in a horizontally scrollable list, with the currently selected 【Android】Chapter 5 (8) Picture Gallery (Galery) placed in the center of the view.
Note: Android has deprecated this widget. The reason for deprecation is that the efficiency of using Galery is relatively low. The official recommendation is to use HorizontalScrollView to replace this widget. However, many 【Android】Chapter 5 (8) Picture Gallery (Galery) browsing functions on mobile phones are currently implemented using Galery. If you still like this widget, you can continue to use it in higher version projects.
2. Example 8--Demo08Gallery
1. Running screenshots
Use the mouse left and right to drag and drop the 【Android】Chapter 5 (8) Picture Gallery (Galery) to observe the effect.
2. Add the Demo08Gallery.axml file
<span style="color: #0000ff;"></span><span style="color: #ff00ff;">xml version="1.0" encoding="utf-8"</span><span style="color: #0000ff;">?></span> <span style="color: #0000ff;"><span style="color: #800000;">LinearLayout </span><span style="color: #ff0000;">xmlns:android</span><span style="color: #0000ff;">="http://schemas.android.com/apk/res/android"</span><span style="color: #ff0000;"> android:orientation</span><span style="color: #0000ff;">="vertical"</span><span style="color: #ff0000;"> android:layout_width</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;"> android:layout_height</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;"> android:gravity</span><span style="color: #0000ff;">="center_vertical"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">Gallery </span><span style="color: #ff0000;">android:id</span><span style="color: #0000ff;">="@+id/gallery"</span><span style="color: #ff0000;"> android:layout_width</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;"> android:layout_height</span><span style="color: #0000ff;">="wrap_content"</span> <span style="color: #0000ff;">/></span> <span style="color: #0000ff;"></span><span style="color: #800000;">LinearLayout</span><span style="color: #0000ff;">></span></span></span>
Save the file and click the [Refresh] button above the Solution Explorer.
3. Add the Demo08Gallery.cs file
<span style="color: #0000ff;">using</span><span style="color: #000000;"> System; </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> Android.App; </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> Android.Content; </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> Android.OS; </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> Android.Views; </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> Android.Widget; </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> java.Lang; </span><span style="color: #0000ff;">namespace</span><span style="color: #000000;"> ch05demos.SrcActivity { [Activity(Label </span>= <span style="color: #800000;">"</span><span style="color: #800000;">Demo08Gallery</span><span style="color: #800000;">"</span><span style="color: #000000;">)] </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Demo08Gallery : Activity { </span><span style="color: #0000ff;">PRotected</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> OnCreate(Bundle savedInstanceState) { </span><span style="color: #0000ff;">base</span><span style="color: #000000;">.OnCreate(savedInstanceState); SetContentView(Resource.Layout.demo08_Gallery); </span><span style="color: #0000ff;">var</span> g = FindViewById<gallery><span style="color: #000000;">(Resource.Id.gallery); g.Adapter </span>= <span style="color: #0000ff;">new</span> ImageAdapter(<span style="color: #0000ff;">this</span><span style="color: #000000;">) { CurrentWidth </span>= <span style="color: #800080;">550</span><span style="color: #000000;">, CurrentHeight </span>= <span style="color: #800080;">550</span><span style="color: #000000;"> }; g.ItemClick </span>+=<span style="color: #000000;"> Gallery_ItemClick; } </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> Gallery_ItemClick(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, AdapterView.ItemClickEventArgs e) { Toast.MakeText(</span><span style="color: #0000ff;">this</span><span style="color: #000000;">, e.Position.ToString(), ToastLength.Short).Show(); } } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> ImageAdapter : BaseAdapter { </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> Context context; </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">int</span>[] thumbIds =<span style="color: #000000;"> { Resource.Drawable.sample_1, Resource.Drawable.sample_2, Resource.Drawable.sample_3, Resource.Drawable.sample_4, Resource.Drawable.sample_5, Resource.Drawable.sample_6, Resource.Drawable.sample_7 }; </span><span style="color: #008000;">//</span><span style="color: #008000;">默认值为500(这是C# 6.0新增的功能,仅VS2015可以这样用)</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> CurrentWidth { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; } = <span style="color: #800080;">500</span><span style="color: #000000;">; </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> CurrentHeight { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; } = <span style="color: #800080;">500</span><span style="color: #000000;">; </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> ImageAdapter(Context c) { context </span>=<span style="color: #000000;"> c; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> Count { </span><span style="color: #0000ff;">get</span> { <span style="color: #0000ff;">return</span><span style="color: #000000;"> thumbIds.Length; } } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> View GetView(<span style="color: #0000ff;">int</span><span style="color: #000000;"> position, View convertView, ViewGroup parent) { ImageView i </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> ImageView(context); i.SetImageResource(thumbIds[position]); i.LayoutParameters </span>= <span style="color: #0000ff;">new</span> Gallery.LayoutParams(<span style="color: #800080;">500</span>, <span style="color: #800080;">500</span><span style="color: #000000;">); i.SetScaleType(ImageView.ScaleType.FitXy); </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> i; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">long</span> GetItemId(<span style="color: #0000ff;">int</span><span style="color: #000000;"> position) { </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">0</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> Java.Lang.Object GetItem(<span style="color: #0000ff;">int</span><span style="color: #000000;"> position) { </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">null</span><span style="color: #000000;">; } } }</span></gallery>
Run.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

OnLeaks has now partnered with Android Headlines to provide a first look at the Galaxy S25 Ultra, a few days after a failed attempt to generate upwards of $4,000 from his X (formerly Twitter) followers. For context, the render images embedded below h

Alongside announcing two new smartphones, TCL has also announced a new Android tablet called the NXTPAPER 14, and its massive screen size is one of its selling points. The NXTPAPER 14 features version 3.0 of TCL's signature brand of matte LCD panels

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

The Vivo Y300 Pro just got fully revealed, and it's one of the slimmest mid-range Android phones with a large battery. To be exact, the smartphone is only 7.69 mm thick but features a 6,500 mAh battery. This is the same capacity as the recently launc

Samsung has not offered any hints yet about when it will update its Fan Edition (FE) smartphone series. As it stands, the Galaxy S23 FE remains the company's most recent edition, having been presented at the start of October 2023. However, plenty of

The Redmi Note 14 Pro Plus is now official as a direct successor to last year'sRedmi Note 13 Pro Plus(curr. $375 on Amazon). As expected, the Redmi Note 14 Pro Plus heads up the Redmi Note 14 series alongside theRedmi Note 14and Redmi Note 14 Pro. Li

Motorola has released countless devices this year, although only two of them are foldables. For context, while most of the world has received the pair as the Razr 50 and Razr 50 Ultra, Motorola offers them in North America as the Razr 2024 and Razr 2
