Android详细教程(基础篇):十六、View组件高级篇:Gallery组件详解

本文介绍Android中的Gallery组件,用于实现图片拖拽浏览功能。讲解了Gallery的继承结构,以及如何通过setAdapter()方法设置图片集,其中适配器有两种实现方式:继承BaseAdapter或使用SimpleAdapter。提供了范例代码展示如何自定义Adapter和使用SimpleAdapter来配置Gallery组件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

7.11. Gallery组件(拖拉组件)

对于许多使用过Android手机的用户应该知道,在Android中可以使用一些软件方便的进行图片的拖拽浏览,

这样的功能就可以通过Gallery实现,使用Gallery组件可以定义一组图片浏览框,可以减轻开发者对于图片

浏览开发的功能的开发困难。

它的继承结构如下:

java.lang.Object

   

android.view.View

 

   

android.view.ViewGroup

 

 

   

android.widget.AdapterView<T extends android.widget.Adapter>

 

 

 

   

android.widget.AbsSpinner

 

 

 

 

   

android.widget.Gallery

 

在使用Gallery设置图片集的时候需要使用setAdapter()方法,此时设置的SpinnerAdapter接口的对象,主

要的功能是定义一组要显示的组件的适配器,而对于这种适配器操作有两种可选方式:

  • 方式一:用一个自定义的类直接继承SpinnerAdapter接口的子类----android.widget.BaseAdapter类实现,

这样用户只需要覆写核心操作方法即可,不需要的方法可以不用覆写。

  • 方式二:直接使用之前学习过的SpinnerAdapter类完成。

SpinnerAdapter接口:

public interface

SpinnerAdapter

implements Adapter

 

已知子类:

Known Indirect Subclasses

ArrayAdapter<T>, BaseAdapter, CursorAdapter, ResourceCursorAdapter, SimpleAdapter, SimpleCursorAdapter

范例一:使用BaseAdapter

配置文件:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

     xmlns:android="http://schemas.android.com/apk/res/android"

     android:id="@+id/MyLayout"

     android:orientation="vertical"

     android:layout_width="fill_parent"

     android:layout_height="fill_parent">

     <Gallery

          android:id="@+id/myGallery"

          android:gravity="center_vertical"

          android:spacing="3px"

          android:layout_width="fill_parent"

          android:layout_height="wrap_content"/>

</LinearLayout>

 

 

自定义Adapter:

package com.makyan.demo;

import android.content.Context;

import android.view.View;

import android.view.ViewGroup;

import android.view.ViewGroup.LayoutParams;

import android.widget.BaseAdapter;

import android.widget.Gallery;

import android.widget.ImageView;

public class ImageGalleryAdapter extends BaseAdapter {

     private Context myContext;                    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值