LinearLayout 不会显示所有内容

发布于 2025-01-02 20:50:43 字数 975 浏览 5 评论 0原文

我的问题看似简单,但我真的无法解决。在某些时候,我的代码会在 AlertDialog 内的 LinearLayout 中显示一堆图像 (ImageView)。 问题是并没有显示所有内容。

这是我的代码:

public  LinearLayout createLayout(String text) {
    LinearLayout ll = new LinearLayout(this.c);

    int res;
    for(int i=0; i<text.length(); ++i) {
        res = this.c.getResources().getIdentifier("pigpen_" + Character.toLowerCase(text.charAt(i)), "drawable", this.c.getPackageName());
        ImageView iv = new ImageView(this.c);
        iv.setPadding(5, 0, 0, 5);
        iv.setImageResource(res);
        ll.addView(iv);
    }
    return ll;
}

DialogAlert 的代码:

protected ResultDialog(final Context context, CharSequence title, LinearLayout ll) {
    super(context);

    ll.setPadding(10, 5, 10, 5);

    this.setView(ll);
    this.setTitle(title);
}

image

正如您所看到的,结果并不好。你知道为什么吗?

My problem might seems easy, but I really can't resolve it. At some point, my code displays a bunch of images (ImageView) in a LinearLayout, inside an AlertDialog.
The problem is that not everything is displayed.

Here's my code :

public  LinearLayout createLayout(String text) {
    LinearLayout ll = new LinearLayout(this.c);

    int res;
    for(int i=0; i<text.length(); ++i) {
        res = this.c.getResources().getIdentifier("pigpen_" + Character.toLowerCase(text.charAt(i)), "drawable", this.c.getPackageName());
        ImageView iv = new ImageView(this.c);
        iv.setPadding(5, 0, 0, 5);
        iv.setImageResource(res);
        ll.addView(iv);
    }
    return ll;
}

And the code for the DialogAlert :

protected ResultDialog(final Context context, CharSequence title, LinearLayout ll) {
    super(context);

    ll.setPadding(10, 5, 10, 5);

    this.setView(ll);
    this.setTitle(title);
}

image

And as you can see, the result is not good. Do you have any idea why?

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

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

发布评论

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

评论(3

岁月静好 2025-01-09 20:50:43

问题是您的 LinearLayout 没有任何尺寸。宽度和高度都是 0。所以一切都很完美。

您缺少对 setLayoutParams 的调用,仅此而已。

ll.setLayoutParams(new LinearLayout.LayoutParams(FILL_PARENT, FILL_PARENT));

The problem is that your LinearLayout does not have any dimensions. Width and height are 0. So everything works perfectly.

You are lacking a call to setLayoutParams, that's all.

ll.setLayoutParams(new LinearLayout.LayoutParams(FILL_PARENT, FILL_PARENT));
幸福不弃 2025-01-09 20:50:43

您可以使用 Horizo​​ntalScrollView 包装 LinearLayout。这样您就可以添加大量图像并滚动以查看全部图像。

You can wrap your LinearLayout with an HorizontalScrollView. That way you can add a lot of images and scroll to view them all.

吾性傲以野 2025-01-09 20:50:43
    LinearLayout ll = new LinearLayout(this.c); 
    ArrayList<Integer> alImages = new ArrayList<Integer>();
    ll.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

    for(int i=0; i<text.length(); ++i) {
        cur = text.charAt(i);
        pos = this.getCharAlphabetPosition(cur);
        if(pos > 0 && pos < alphabetId.length) {
            res = alphabetId[pos];
            alImages.add(res);
        }
    }
    gv.setAdapter(new ImageAdapter(this.c, alImages));
    gv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

    gv.setNumColumns(GridView.AUTO_FIT);
    gv.setColumnWidth(25);

    ll.addView(gv);
}

ImageAdapter 类:
公共类 ImageAdapter 扩展 BaseAdapter 实现 ListAdapter {
私有上下文c;
私有ArrayList alImages;

public ImageAdapter(Context c, ArrayList<Integer> alImages) {
    this.c = c;
    this.alImages = alImages;
}

public int getCount() {
    return this.alImages.size();
}

public Integer getItem(int position) {
    return this.alImages.get(position);
}

public long getItemId(int position) {
    return 0;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(this.c);
        imageView.setLayoutParams(new GridView.LayoutParams(25, 25));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(5, 0, 0, 5);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(this.getItem(position));
    return imageView;
}

}
    LinearLayout ll = new LinearLayout(this.c); 
    ArrayList<Integer> alImages = new ArrayList<Integer>();
    ll.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

    for(int i=0; i<text.length(); ++i) {
        cur = text.charAt(i);
        pos = this.getCharAlphabetPosition(cur);
        if(pos > 0 && pos < alphabetId.length) {
            res = alphabetId[pos];
            alImages.add(res);
        }
    }
    gv.setAdapter(new ImageAdapter(this.c, alImages));
    gv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

    gv.setNumColumns(GridView.AUTO_FIT);
    gv.setColumnWidth(25);

    ll.addView(gv);
}

Et ma classe ImageAdapter :
public class ImageAdapter extends BaseAdapter implements ListAdapter {
private Context c;
private ArrayList alImages;

public ImageAdapter(Context c, ArrayList<Integer> alImages) {
    this.c = c;
    this.alImages = alImages;
}

public int getCount() {
    return this.alImages.size();
}

public Integer getItem(int position) {
    return this.alImages.get(position);
}

public long getItemId(int position) {
    return 0;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(this.c);
        imageView.setLayoutParams(new GridView.LayoutParams(25, 25));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(5, 0, 0, 5);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(this.getItem(position));
    return imageView;
}

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文