在imageswitcher中显示文本

时间:2012-09-05 18:21:26

标签: android android-layout android-intent

我的应用程序基于电子商务。我正在使用图库。我想在图库中的每个图像下面添加文本和按钮,当我点击尊重的图像时显示。文本与不同的产品有关每个图像和按钮用于移动另一个活动。

代码如下。我是android的新手。非常感谢帮助。

代码:

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class GalleryView extends Activity {
Integer[] pics = {
    R.drawable.antartica1,
    R.drawable.antartica2,
    R.drawable.antartica3,
    R.drawable.antartica4,
    R.drawable.antartica5,
    R.drawable.antartica6,
    R.drawable.antartica7,
    R.drawable.antartica8,
    R.drawable.antartica9,
    R.drawable.antartica10
   };
  ImageView imageView;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Gallery ga = (Gallery)findViewById(R.id.Gallery01);
ga.setAdapter(new ImageAdapter(this));

imageView = (ImageView)findViewById(R.id.ImageView01);
ga.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        Toast.makeText(getBaseContext(), 
                "You have selected picture " + (arg2+1) + " of Antartica", 
                Toast.LENGTH_SHORT).show();
        imageView.setImageResource(pics[arg2]);

    }

});

}


 public class ImageAdapter extends BaseAdapter {

private Context ctx;
int imageBackground;

public ImageAdapter(Context c) {
    ctx = c;
    TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
    imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
    ta.recycle();
}

public int getCount() {

    return pics.length;
}

public Object getItem(int arg0) {

    return arg0;
}

public long getItemId(int arg0) {

    return arg0;
}

public View getView(int arg0, View arg1, ViewGroup arg2) {
    ImageView iv = new ImageView(ctx);
    iv.setImageResource(pics[arg0]);
    iv.setScaleType(ImageView.ScaleType.FIT_XY);
    iv.setLayoutParams(new Gallery.LayoutParams(150,120));
    iv.setBackgroundResource(imageBackground);
    return iv;
}

}
 }

1 个答案:

答案 0 :(得分:2)

修改您的ImageAdapter类

LayoutInflater mInflater;

在ImageAdapter()

中初始化它
   mInflater=LayoutInflater.from(c);

现在修改getView()

public View getView(int arg0, View arg1, ViewGroup arg2) {
View v=mInflater.inflate(R.layout.your_layout, null);
  

ImageView iv =(ImageView)v.findViewById(R.id.imave_view);

TextView tv=(TextView)v.findViewById(R.id.tv);
  

tv.setTxt(//设置你的文字);   iv.setImageResource(图片[arg0中]);       iv.setScaleType(ImageView.ScaleType.FIT_XY);       iv.setLayoutParams(new Gallery.LayoutParams(150,120));       iv.setBackgroundResource(imageBackground);       返回v;   }