我在Android中編寫圖庫視圖時遇到此問題。這些圖像出現在屏幕的頂部屏幕(如預期的那樣),當它們被點擊時,它們會說出選擇了哪個圖像(如預期的那樣)。但是,所選圖像不會顯示在imageView中。我的代碼如下:當圖像選擇時,圖庫視圖將不會顯示完整的ImageView
public class MainActivity extends Activity {
Integer[] imageIDs = {
R.drawable.fourfromunimon,
R.drawable.fourfromunisat,
R.drawable.fourfromunisun,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener()
{
@SuppressWarnings("rawtypes")
public void onItemClick(AdapterView parent, View v,
int position, long id)
{
Toast.makeText(getBaseContext(),
"pic" + (position + 1) + " selected",
Toast.LENGTH_SHORT).show();
// Display the image
ImageView imageView =
(ImageView) findViewById(R.id.image1);
imageView.setImageResource(imageIDs[position]);
}
});
}
public class ImageAdapter extends BaseAdapter {
Context context;
int itemBackground;
public ImageAdapter(Context c)
{
context = c;
TypedArray a = obtainStyledAttributes(
R.styleable.Gallery1);
itemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground,
0);
a.recycle();
}
public int getCount(){
return imageIDs.length;
}
public Object getItem(int position){
return position;
}
public long getItemId(int position){
return position;
}
public View getView(int position, View convertView,
ViewGroup parent){
ImageView imageView;
if (convertView == null){
imageView = new ImageView(context);
imageView.setImageResource(imageIDs[position]);
imageView.setScaleType(
ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(
new Gallery.LayoutParams(450, 200));
}else{
imageView = (ImageView) convertView;
}
imageView.setBackgroundResource(itemBackground);
return imageView;
}
}
}
任何幫助將不勝感激,因爲我沒有在那裏我可以去錯了foggiest想法。
是ImageView與android:id =「image1」目前在activity_main.xml? – Akhil 2013-04-08 19:11:02
爲什麼每次點擊圖庫項目時都會發現視圖?檢查:'ImageView imageView =(ImageView)findViewById(R.id.image1);' – 2013-04-08 19:13:52