Android实战简易教程-第十枪(画廊组件Gallery实用研究)

本文介绍了如何在Android中实现Gallery组件用于拖拽浏览图片,详细讲解了Gallery的实现步骤,包括布局文件设置和自定义适配器。同时,文章还探讨了Gallery与ImageSwitcher的结合使用,通过SimpleAdapter实现更丰富的展示效果。
摘要由CSDN通过智能技术生成

Gallery组件用于拖拽浏览图片,下面我们就来看一下如何实现。

一、实现Gallery

1.布局文件很简单:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/MyLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <Gallery
        android:id="@+id/myGallery"
        android:gravity="center_vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>


2.自定义适配器类,可以直接覆写BaseAdapter类中的几个方法。

package org.yayun.demo;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Gallery.LayoutParams;

public class ImageGalleryAdapter extends BaseAdapter {
	private Context context;
	private int imgRes[] = new int[] { R.drawable.ispic_a, R.drawable.ispic_b,
			R.drawable.ispic_c, R.drawable.ispic_d, R.drawable.ispic_e, };

	public ImageGalleryAdapter(Context c) {//构造方法,用于获取上下文对象
		this.context = c;
	}

	public int getCount() {

		return imgRes.length;
	}

	public Object getItem(int position) {
		return imgRes[position];
	}

	public long getItemId(int position) 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值