gallery.xml:

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:layout_height="wrap_content"

android:text="这是一个Gallery画廊控件的案例"/>

android:layout_width="fill_parent"

android:layout_height="fill_parent"/>

GalleryActivity.java:

package com.example.baseexample;

import android.app.Activity;

import android.content.Context;

import android.os.Bundle;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.Gallery;

import android.widget.ImageView;

public class GalleryActivity extends Activity {

private Gallery mGallery;

public void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.gallery);

mGallery = (Gallery)findViewById(R.id.mygallery);

mGallery.setAdapter(new ImageAdapter(this));

}

private class ImageAdapter extends BaseAdapter{

private Context mContext;

private Integer[] mImage = {R.drawable.a,

R.drawable.b,

R.drawable.c,

R.drawable.d,

R.drawable.e,

R.drawable.f};

public ImageAdapter(Context c){

mContext = c;

}

public int getCount(){

return mImage.length;

}

public Object getItem(int position){

return position;

}

public long getItemId(int position){

return position;

}

public View getView(int position,View converView,ViewGroup parent){

ImageView i = new ImageView(mContext);

i.setImageResource(mImage[position]);

i.setScaleType(ImageView.ScaleType.FIT_XY);

i.setLayoutParams(new Gallery.LayoutParams(600, 1000));

return i;

}

}

}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐