简体   繁体   English

如何在RelativeyLayout中以编程方式将视图放置在另一个视图下?

[英]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 . 我想要v1OVERLAP 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);

You can add Rule to your RelativeLayout . 您可以将Rule添加到您的RelativeLayout Say for ex: 说前:

   p.addRule(RelativeLayout.ALIGN_BOTTOM, existingView.getId());

The above rule will allign the new view below the existing view. 上面的规则将在现有视图下方引用新视图。 You can read more about this here 您可以在此处了解更多信息

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

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