简体   繁体   English

如何在 RelativeLayout 中显示一些 ImageView?

[英]How display some ImageView in a RelativeLayout?

I have a RelativeLayout我有一个相对布局

RelativeLayout myLayout = new RelativeLayout(this);

Then, I add 2 ImageView in this Layout:然后,我在此布局中添加 2 ImageView:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(20, 20);

ImageView img1 = new ImageView(this);
// Here I give the position of img1

ImageView img2 = new ImageView(this);
// And here the position and img2

myLayout.addView(img1, params);
myLayout.addView(img2, params);
setContentView(myLayout);

And here I have a problem: I want to show and click on the 2 ImageViews, but only the img2 is visible on my screen.在这里我有一个问题:我想显示并单击 2 ImageViews,但我的屏幕上只有 img2 可见。

Is there a problem with the z-index or something else? z-index 或其他问题是否存在问题?

Since you are using RelativeLayout and same params for both ImageView one will be overlapped with the other.由于您使用的是 RelativeLayout 并且params的参数相同,因此一个将与另一个重叠。 So define params for both.所以为两者定义参数。 Add rule to each param for positioning it.为每个参数Add rule以定位它。 And then give addView() .然后给addView()

For eg:例如:

RelativeLayout myLayout = new RelativeLayout(this);

ImageView img1 = new ImageView(this);
RelativeLayout.LayoutParams firstImageParams = new RelativeLayout.LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
leftArrowParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

ImageView img2 = new ImageView(this);
RelativeLayout.LayoutParams secImageParams = new RelativeLayout.LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
rightArrowParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

myLayout.addView(img1, firstImageParams);
myLayout.addView(img2, secImageParams);
setContentView(myLayout);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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