如何消除 LinearLayout 中视图之间的空间
我有一个 HorizontalScrollView,其中包含一个水平 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的图像正在缩放,因此您需要在图像对象上调用 adjustmentViewBounds(true) 以删除可用的水平空间。
Your images are getting scaled, so you need to call adjustViewBounds(true) on your image objects to remove the free horizontal space.
可能需要将边距设置为 0。如下所示:
Probably need to set the margins to 0. Something like this: