我遵循hello gallery教程,但不起作用。它顯示空白屏幕。Android Hello Gallery不支持
我已經將示例圖像上傳到可繪製文件夾中。
這裏是main.xml中代碼
<?xml version="1.0" encoding="utf-8"?>
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
的attr.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="HelloGallery">
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>
的活動類:
public class GalleryActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(GalleryActivity.this, "" + position,
Toast.LENGTH_SHORT).show();
}
});
}
}
ImageAdapter類:
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
Context mContext;
private Integer[] mImageIds = { R.drawable.sample_0,
R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6,
R.drawable.sample_7 };
public ImageAdapter(Context context) {
mContext = context;
TypedArray attr = mContext
.obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = attr.getResourceId(
R.styleable.HelloGallery_android_galleryItemBackground, 0);
attr.recycle();
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mImageIds[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
}
當我調試的代碼,我注意到,該方法getView從未執行過。 請求幫助。
感謝
謝謝。有用。我認爲這種方法並不重要。 – user1134475 2012-03-16 01:36:50