如何消除 LinearLayout 中视图之间的空间

发布于 2024-12-01 16:32:35 字数 1644 浏览 2 评论 0原文

我有一个 Horizo​​ntalScrollView,其中包含一个水平 LinearLayout。现在我将同样大的 ImageView 添加到 LinearLayout 中,以便我可以在 ImageView 之间滚动。 ImageViews 应该紧密地并排放置,它们之间没有空间,但是每个图像的两侧都形成了巨大的空间(我认为它是图像宽度的一半)。

这是我的做法,希望有人给我提示。

public class SnapGallery extends HorizontalScrollView {

    private ArrayList<Bitmap> items = null; 

    protected Context context = null; //Context of the activity
    protected LinearLayout wrapper = null; //This serves as a container for the images in the SnapGallery.

    //Constructor
    public SnapGallery(Context context) {
        super(context);

        this.context = context;
        items = new ArrayList<Bitmap> ();

        this.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        wrapper = new LinearLayout(context);
        wrapper.setOrientation(LinearLayout.HORIZONTAL);
        wrapper.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        wrapper.setPadding(0, 0, 0, 0);
        wrapper.setGravity(Gravity.LEFT);

        this.addView(wrapper);

        items = getTestBitmaps();

        for (Bitmap b : items) {
            ImageView curr = createViewFromBitmap(b);
            wrapper.addView(curr, new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1.0f));
        }

    }

    public ImageView createViewFromBitmap (Bitmap bitmap) {

        ImageView view = new ImageView(context);
        view.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        view.setPadding(0, 0, 0, 0);
        view.setImageBitmap(bitmap);
        return view;        
    }
}

提前致谢!

I have a HorizontalScrollView which contains a horizontal LinearLayout. Now I am adding equally big ImageViews to the LinearLayout so that i can scroll between the ImageViews. The ImageViews should lay tightly side by side with no space between them, but there develops a huge space on both sides of each image (i think its half the width of the images).

Here is how I did this, I hope someone has a tip for me.

public class SnapGallery extends HorizontalScrollView {

    private ArrayList<Bitmap> items = null; 

    protected Context context = null; //Context of the activity
    protected LinearLayout wrapper = null; //This serves as a container for the images in the SnapGallery.

    //Constructor
    public SnapGallery(Context context) {
        super(context);

        this.context = context;
        items = new ArrayList<Bitmap> ();

        this.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        wrapper = new LinearLayout(context);
        wrapper.setOrientation(LinearLayout.HORIZONTAL);
        wrapper.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        wrapper.setPadding(0, 0, 0, 0);
        wrapper.setGravity(Gravity.LEFT);

        this.addView(wrapper);

        items = getTestBitmaps();

        for (Bitmap b : items) {
            ImageView curr = createViewFromBitmap(b);
            wrapper.addView(curr, new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1.0f));
        }

    }

    public ImageView createViewFromBitmap (Bitmap bitmap) {

        ImageView view = new ImageView(context);
        view.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        view.setPadding(0, 0, 0, 0);
        view.setImageBitmap(bitmap);
        return view;        
    }
}

Thanks in advance!

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

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

发布评论

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

评论(2

凉墨 2024-12-08 16:32:35

您的图像正在缩放,因此您需要在图像对象上调用 adjustmentViewBounds(true) 以删除可用的水平空间。

Your images are getting scaled, so you need to call adjustViewBounds(true) on your image objects to remove the free horizontal space.

不回头走下去 2024-12-08 16:32:35

可能需要将边距设置为 0。如下所示:

LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(0, 0, 0, 0);
wrapper.setLayoutParams(params);

Probably need to set the margins to 0. Something like this:

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