[英]How can I place a View under another View programmatically in RelativeyLayout?
If I add two View
to the RelativeLayout
, the newer one overlaps the older one. 如果我将两个
View
添加到RelativeLayout
,则较新的View
将与较旧的View
重叠。 But for some reasons, I cannot add the View
that I want to place at the bottom first. 但是由于某些原因,我无法添加要放在底部的
View
。 Could anyone tell me how I can solve this? 谁能告诉我如何解决这个问题?
RelativeLayout rela = (RelativeLayout) findViewById(...);
rela.addView(v1);
rela.addView(v2);
I want v1
to OVERLAP v2
. 我想要
v1
到OVERLAP v2
。 I cannot use other Layout
because this is one part of a large project. 我不能使用其他
Layout
因为这是大型项目的一部分。
Since I have not got a correct answer. 由于我没有正确的答案。 I have to rewrite the whole program.
我必须重写整个程序。 This is what I did: I added
v2
first, and then v1
. 这就是我所做的:我先添加了
v2
,然后又添加了v1
。 Then I just set the visibility of v1
to View.Gone
. 然后,我只是将
v1
的可见性设置为View.Gone
。 I do not know if there is a better way to solve this. 我不知道是否有更好的方法来解决这个问题。
如果您想在其他视图上添加视图...。那么您可以使用FrameLayout...。在RelativeLayout中...希望这对您有所帮助。
If you want to add a view on bottom of other view. 如果要在其他视图的底部添加一个视图。 You can use LinearLayout (in vertical).
您可以使用LinearLayout(垂直)。 But if you want to add view to bottom of RelativeLayout.
但是,如果要将视图添加到RelativeLayout的底部。 Try this:
尝试这个:
First, your RelativeLayout needs an ID if you want to reference it: 首先,如果要引用,您的RelativeLayout需要一个ID:
RelativeLayout rLayout = (RelativeLayout)findViewById(R.id.yourRelativeId);
Then create some LayoutParams for the object (in this case, your admob adview) that tell it to align itself to the bottom (and not align to any other views, this way it isn't pushed off-screen or moved by the other views): 然后为该对象(在本例中为admob adview)创建一些LayoutParams,以告诉该对象使其自身与底部对齐(而不与任何其他视图对齐),这样就不会将其推离屏幕或被其他视图移动):
RelativeLayout.LayoutParams rLParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
rLParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);
Next, add the view to your RelativeLayout with your LayoutParams: 接下来,使用LayoutParams将视图添加到您的RelativeLayout中:
rLayout.addView(yourAdView, rLParams);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.