android之路Gallery 畫廊

來源:互聯網
上載者:User

標籤:選擇   etl   ast   ==   imageview   back   nts   ack   元素定位   

Gallery是一個內部元素能夠水平滾動,而且能夠把當前選擇的子項目定位在它中心的布局組件。

我們還是直接看看範例的執行效果。


1.新疆項目HelloGallery

2.把須要展示的圖片放入res/drawable檔案夾。woman01.jpg。woman02.jpg,woman03.jpg

3.res/layout/activity_main.xml檔案的內容例如以下:

<FrameLayout  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:id="@+id/FrameLayout01"   ><ImageView android:layout_width="match_parent"    android:layout_height="match_parent"    android:id="@+id/ImageView01"    android:src="@drawable/woman01"/><Gallery android:layout_width="match_parent"    android:layout_height="wrap_content"    android:id="@+id/gallery01"    android:spacing="5dp"/></FrameLayout >

我們使用FrameLayout來實現疊加效果,使用ImageView來顯示大圖。Gallery來展示畫廊,android:spacing="5dp" 屬性則是用來設定元素之間的間隔。


4.在res/values/檔案夾中建立一個attrs.xml內容例如以下:

<?xml version="1.0" encoding="utf-8"?><resources>   <declare-styleable name="HelloGallery">         <attr name="android:galleryItemBackground">        </attr>   </declare-styleable></resources>
5.主activity,MainActivity的代碼例如以下:

<pre name="code" class="java">package com.howlaa.hellogallery;import android.os.Bundle;import android.app.Activity;import android.view.Menu;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;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);final ImageView iv = (ImageView)findViewById(R.id.ImageView01);Gallery g = (Gallery) findViewById(R.id.gallery01);//設定圖片匹配器        g.setAdapter(new ImageAdapter(this));        //設定AdapterView點擊監聽器,Gallery是AdapterView的子類        g.setOnItemClickListener(new OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view,                            int position, long id) {                    //顯示點擊的是第幾張圖片                    Toast.makeText(MainActivity.this, "" + position,                                    Toast.LENGTH_LONG).show();                    //設定背景部分的ImageView顯示當前Item的圖片                    iv.setImageResource(((ImageView)view).getId());            }    });} }


6.ImageAdapter的代碼例如以下:


package com.howlaa.hellogallery;import android.content.Context;import android.content.res.TypedArray;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;import android.widget.SpinnerAdapter;public class ImageAdapter extends BaseAdapter {//Item的修飾背景    int mGalleryItemBackground;    //內容物件    private Context mContext;    //圖片數組    private Integer[] mImageIds ={R.drawable.woman01,R.drawable.woman02,R.drawable.woman03};  //構造方法    public ImageAdapter(Context c){            mContext = c;    //讀取styleable資源    TypedArray a = mContext.obtainStyledAttributes(R.styleable.HelloGallery);    mGalleryItemBackground = a.getResourceId(            R.styleable.HelloGallery_android_galleryItemBackground, 0);    a.recycle();    }        @Overridepublic int getCount() { return mImageIds.length;}@Overridepublic Object getItem(int position) { return position;}@Overridepublic long getItemId(int position) {  return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ImageView iv = new ImageView(mContext);iv.setImageResource(mImageIds[position]);  //給產生的ImageView設定Id,不設定的話Id都是-1        iv.setId(mImageIds[position]);        iv.setLayoutParams(new Gallery.LayoutParams(120, 160));        iv.setScaleType(ImageView.ScaleType.FIT_XY);        iv.setBackgroundResource(mGalleryItemBackground);return iv;}}


android之路Gallery 畫廊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.