我正在尝试在我的 Android 项目中创建一个画廊,但似乎无法使其正常工作。
这是我正在尝试做的事情:
基本上,我想要一个只显示一张图片的画廊,但仍然具有相同的控件(左右滑动)。我从互联网上异步获取图片。
我在哪里?
好吧,现在我只是想显示画廊......我会在之后看到“一张图片”。
我的问题是什么?
嗯...我的画廊没有显示在我的布局上。完全没有。
似乎是什么问题?
日志信息告诉我我的画廊对象的高度和宽度是...... 0。
说到代码...
布局.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:background="@drawable/fond">
<ImageView android:background="@drawable/banniere" android:layout_height="wrap_content" android:layout_width="fill_parent"/>
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
活动
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.galleryfullscreen);
Gallery gal = (Gallery) findViewById(R.id.gallery);
ArrayList<Photo> photos = (ArrayList<Photo>) this.getIntent().getExtras().getSerializable("Photos");
int pos = this.getIntent().getExtras().getInt("Index");
Log.i("Season",photos.toString());
gal.setAdapter(new PhotoAdapter(this, photos, false));
gal.setSelection(pos);
Log.d("Bottom", String.valueOf(gal.getBottom()));
Log.d("Right", String.valueOf(gal.getRight()));
Log.d("Width", String.valueOf(gal.getWidth()));
Log.d("Height", String.valueOf(gal.getHeight()));
}
附加设置正确。
适配器
公共类 PhotoAdapter 扩展 BaseAdapter {
private ArrayList<PhotoView> views;
private ArrayList<Photo> season;
public PhotoAdapter(Context c, ArrayList<Photo> images, boolean mini) {
season = images;
views = new ArrayList<PhotoView>();
for (int i = 0; i < images.size() ; i++)
if (mini)
views.add(new PhotoView(c,images.get(i).getUrlMini(),false));
else{
views.add(new PhotoView(c,images.get(i).getUrlBig(),true));
}
}
@Override
public int getCount() {
return views.size();
}
@Override
public Object getItem(int position) {
return views.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return views.get(position);
}
public ArrayList<Photo> getSeason() {
return season;
}
}
此适配器适用于其他对象(例如 gridview)
和照片视图...
公共类 PhotoView 扩展 ImageView {
Bitmap image;
public PhotoView(Context context, String url, boolean Gallery) {
super(context);
if (!Gallery){
this.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.WRAP_CONTENT,GridView.LayoutParams.WRAP_CONTENT));
this.setScaleType(ImageView.ScaleType.FIT_CENTER);
this.setImageDrawable((getResources().getDrawable(R.drawable.blk)));
new DownloadImageTask().execute(url);
}
else{
this.setLayoutParams(new Gallery.LayoutParams(100,100));
this.setScaleType(ImageView.ScaleType.FIT_CENTER);
this.setBackgroundDrawable(((getResources().getDrawable(R.drawable.blk))));
this.setImageDrawable((getResources().getDrawable(R.drawable.blk)));
}
}
public Bitmap getImage(){
return image;
}
public void setImage(Bitmap img){
image = img;
this.setImageBitmap(image);
}
同样,它适用于网格视图。对于下载任务,还没有为画廊设置。我至少在设置之前尝试显示资源,所以我确定问题不是来自这里。
Sooo...为什么我的日志显示画廊宽度和高度为“0”?为对象(我尝试了 XML 和代码)和内部视图设置了 LayoutParams。创建项目,并调用“getView”方法。我尝试在这里设置 LayoutParams,并没有改变任何东西。
我还尝试在画廊的布局中添加 xmlns:android="http://schemas.android.com/apk/res/android" 部分,并没有改变任何事情。
我真的不知道我错过了什么。我试图改编许多关于画廊对象的教程,但仍然......不知道!如果您对我应该做什么/尝试有任何想法,请...
谢谢大家!
干杯