画廊布局问题

发布于 2024-09-28 04:26:38 字数 4072 浏览 3 评论 0原文

我正在尝试在我的 Android 项目中创建一个画廊,但似乎无法使其正常工作。

这就是我想要做的:

基本上,我想要一个仅显示一张图片的画廊,但仍然具有相同的控件(左右滑动)。我从互联网上异步获取图片。

我现在在哪里?

好吧,现在我只是想显示图库...我将在后面看到“一张图片”。

我的问题是什么?

嗯...我的画廊没有显示在我的布局上。完全没有。

似乎是什么问题?

日志信息告诉我,我的画廊对象的高度和宽度是... 0。

谈论代码...

Layout.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()));       
}

附加已正确设置。

适配器

public class PhotoAdapter extends 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...

public class PhotoView extends 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);
}

同样,对于网格视图也可以正常工作。对于下载任务,尚未为图库设置。我至少在设置之前尝试显示资源,所以我确信问题不是来自这里。

太好了...为什么我的日志显示画廊宽度和高度为“0”? LayoutParams 是为对象(我尝试了 XML 和代码)和内部视图设置的。创建项目并调用“getView”方法。我尝试在这里设置 LayoutParams,但没有改变任何内容。

我还尝试在画廊的布局中添加 xmlns:android="http://schemas.android.com/apk/res/android" 部分,但没有改变任何东西。

我真的不知道我错过了什么。我尝试改编许多关于画廊对象的教程,但仍然......不知道! 如果您知道我应该做什么/尝试什么,请...

谢谢大家!

干杯

I'm trying to create a gallery in my Android project, but can't seem to make it work as it should.

Here is what I am trying to do:

Basically, I want a gallery that displays only one picture, but still has the same controls (sliding left and right). I'm getting the pictures asynchronously from the internet.

Where am I at that?

Well, for now I'm just trying to display the gallery... I'll see the "one picture" step after.

What is my problem?

Well... My gallery doesn't show on my layout. At all.

What seems to be the problem?

Log information give me that the height and the widht of my gallery object are... 0.

Speaking about code...

Layout.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>

Activity

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()));       
}

Extras are correctly set.

Adapter

public class PhotoAdapter extends 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;
}

}

This adaptor is working fine for other objects (such as gridview)

And the PhotoView...

public class PhotoView extends 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);
}

The same, it's working fine for a grid view. With the download task, that is not set up for the Gallery yet. I'm at least trying to display a ressource before setting this up, so I'm sure the problem doesn't come from here.

Sooo... why are my logs showing "0" for gallery width and height? LayoutParams are set for the object (I tried both XML and code), and for the views inside. The items are created, and the "getView" method is called. I tried seeting up LayoutParams here, doesn't change anything.

I also tried adding the xmlns:android="http://schemas.android.com/apk/res/android" part in the layout for the gallery, doesn't change a thing.

I really don't know what I'm missing. I tried to adapt many tutorials on gallery objects, but still... don't know!
If you have any idea of what I should do/try, please...

Thanks all!

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

习ぎ惯性依靠 2024-10-05 04:26:38

通过查看您的 XML 文件,我可以看出可能存在什么问题。您的 LinearLayout 设置为在水平方向上添加视图(默认),并且您的第一个视图的layout_width 设置为 fill_parent。

在 LinearLayout 中将方向设置为垂直。

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" 
  android:orientation="vertical"
  android:background="@drawable/fond">

Looking at your XML file I can see what could be problem. Your LinearLayout is set to add views in an horizontal matter (that is default) and your first view has its layout_width set to fill_parent.

Set orientation to vertical in your LinearLayout.

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" 
  android:orientation="vertical"
  android:background="@drawable/fond">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文