Android自定义Gallery布局

首先,创建一个简单的只包含gallery的布局文件grallery.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Gallery
        android:id="@+id/gallery"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="30dp" />
</LinearLayout>


 

然后在创建一个相关联的Activity类GralleryDemo,在gallery里面通过setAdapter方法用自定义的ImageAdapter加载进布局

package com.example.grallerydemo;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.Toast;

public class GralleryDemo extends Activity {
    
	private Gallery gallery;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.grallery);
		/**
		 * 初始化gallery
		 */
		gallery=(Gallery) findViewById(R.id.gallery);
		try {
			gallery.setAdapter(new ImageAdapter(GralleryDemo.this));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		//单击事件
		gallery.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int itemid,
					long arg3) {
				// TODO Auto-generated method stub
				Toast.makeText(GralleryDemo.this, ""+itemid,Toast.LENGTH_LONG).show();
			}
		});
	}
 
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.grallery_demo, menu);
		return true;
	}

}

最后,设计好你自己定义的ImageAdapter就OK了。

package com.example.grallerydemo;

import android.content.Context;
import android.database.DataSetObserver;
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 {
    
	
	private Context context;
	private Integer image[]={
		    R.drawable.ic_launcher,
		    R.drawable.ic_launcher,
		    R.drawable.ic_launcher,
		    R.drawable.ic_launcher,
		    R.drawable.ic_launcher
	};
	public ImageAdapter(Context c) {
		// TODO Auto-generated constructor stub
		context=c;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		//return image.length;
	    return Integer.MAX_VALUE;
	}
	
    
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;   //获取图像在图库中的位置
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public int getItemViewType(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ImageView imageview = new ImageView(context);
		//给imageView设置图像资源
		//imageview.setImageResource(image[position]);
		  imageview.setImageResource(image[position%image.length]);
		//设置比例类型
		imageview.setScaleType(ImageView.ScaleType.FIT_XY);
		//设置图像布局和显示大小
		imageview.setLayoutParams(new Gallery.LayoutParams(100, 100));
		//设置图像之间的距离
		imageview.setPadding(15, 0, 15, 0);
		return imageview;
	}

	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void registerDataSetObserver(DataSetObserver arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void unregisterDataSetObserver(DataSetObserver arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public View getDropDownView(int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		return null;
	}

}


最终是循环显示,效果如下图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值