开发者

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 SnapGal开发者_运维百科lery(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!


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜