简体   繁体   English

如何确保视图放置在相对布局中时不重叠

[英]How to make sure views dont overlap when placed in a Relative Layout

I use a RelativeLayout to place views using LayoutParams and setting the top (y coordinate) and left margin (x coordinate). 我使用RelativeLayout使用LayoutParams放置视图,并设置顶部(y坐标)和左边距(x坐标)。 How can I make sure that when I place views using absolute position that none of them overlap each other? 当我使用绝对位置放置视图时,如何确保它们之间没有重叠? I do run into overlapping issues in some spots and the only work around I have right now is to scale up the y values. 我确实在某些地方遇到了重叠的问题,而现在我唯一能解决的就是扩大y值。

Here's my code to place the views: 这是放置视图的代码:

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

lp.leftMargin = x;
lp.topMargin = y;

then once I create the view I place it in the layout at the correct spot: 然后,一旦创建视图,便将其放置在布局中的正确位置:

if(field.getType().equalsIgnoreCase("label"))
{
  CSLabel label = (CSLabel) field;
  TextView tv = new TextView(this);
  tv.setText(Html.fromHtml(label.getText()));

  rscroll.addView(tv, lp);
}

Here's a picture of what happens though sometimes: 这是有时发生的情况的图片:

在此处输入图片说明

The textview and radiogroup in this case are overlapping. 在这种情况下,textview和radiogroup是重叠的。 Are there any ways to fix overlapping programmatically? 有什么方法可以以编程方式解决重叠问题吗? Thank you. 谢谢。

You don't most likely want to use x/y coordinates to set the position of your views, instead use alignTop / alignBottom layout parameters to set the position relative to other view, which will ensure that no overlapping is going to happen! 您不太可能希望使用x / y坐标来设置视图的位置,而是使用alignTop / alignBottom布局参数来设置相对于其他视图的位置,这将确保不会发生重叠!

Also please consider using different layouts, such as LinearLayout with Vertical orieantation, its much more efficient in simple cases such as yours. 另外,请考虑使用其他布局,例如带有Vertical orieantation的LinearLayout,在诸如您这样的简单情况下,它的效率要高得多。

Also please consider defining layout in XML instead of Java code. 另外,请考虑使用XML而不是Java代码定义布局。

Either restrict the radio button to stay below the text view or restrict the text view to stay above the radio button. 限制单选按钮停留在文本视图下方,或者限制文本视图停留在单选按钮上方。

Use the addRule(int, int) method of layout params and RelativeLayout.BELOW or RelativeLayout.ABOVE. 使用布局参数和RelativeLayout.BELOW或RelativeLayout.ABOVE的addRule(int,int)方法。

http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#addRule(int,int) http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#addRule(int,int)

Use align_top, layout_below and other properties provided by Relative Layout to align your views.I would also recommend you to align your layouts via XML and not programmatically. 使用``相对布局''提供的align_top,layout_below和其他属性来对齐视图。我也建议您通过XML而不是通过编程来对齐布局。

Read this for more info on relative layouts http://developer.android.com/intl/pt-br/reference/android/widget/RelativeLayout.html 阅读此书以获得有关相对布局的更多信息http://developer.android.com/intl/pt-br/reference/android/widget/RelativeLayout.html

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

相关问题 将相对布局的透明前景重叠,以使文本视图在相对布局内部变为灰色透明 - Overlap a transparent foreground of a Relative layout to make text views color gray transparent inside Relative layout 放置在相对布局中时,Admob横幅不显示 - Admob banner not appearing when placed in relative layout 如何在选择时进行划分视图和显示颜色的布局? - How to make layout which divides views and shows colored when selected? 如何在相对布局或任何布局中动态添加视图 - How can I add Views dynamically in a Relative layout or in any Layout 防止TextView和ImageView在相对布局中重叠 - Preventing TextView and ImageView Overlap In Relative Layout 如何使用框阴影进行相对布局 - How to make relative layout with box shadow 放置在布局中时如何将SDLSurfaceView置于最前面? - How to bring SDLSurfaceView to front when placed inside a layout? Android ADT-Eclipse | 在视图/布局文件中编辑或添加ID时不要更改R.java - Android ADT - Eclipse | Dont changes R.java when editing or adding Ids in views / layout files 如何确保活动的布局不会被其他活动替代? - How to make sure the layout of an activity no replace by other activity? 如何确保在JFrame上随机位置绘制的点不会与已经绘制的形状重叠? - How to make sure that points drawn on JFrame at random location does not overlap already drawn shapes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM