Android中Gallery(画廊)的使用
1、Gallery
1.1、使用BaseAdapter完成Gallery
思路:通过BaseAdapter类加载一个布局文件信息。把一个布局问价转换成一个View,加载到Gallery中。步骤:-->新建一个布局文件gallery_layout.xml,其中只有一个Gallery组件。-->新建一个布局文件gallery_item_layout.xml,其中只有一个ImageView组件。-->新建一个GalleryDemoActivity类,此类继承自Activity。-->新建一个GalleryDemoAdapter类,此类继承自BaseAdapter。1)、建立gallery_layout.xml,代码如下:2)、建立gallery_item_layout.xml,其中只有一个ImageView组件,该组件用于显示图片。<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/RelativeLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Gallery android:id="@+id/gly_showglyimg" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:spacing="2dp" /> </RelativeLayout>
3)、新建一个GalleryDemoAdapter,继承自BaseAdapter类,并覆写相应方法。<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/iv_showglyimg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher"/> </LinearLayout>
package com.example.adapter; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import com.example.highcomponent.R; public class GalleryDemoAdapter extends Ba