简体   繁体   中英

How to get rid of space between Views in LinearLayout

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!

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM