简体   繁体   English

RelativeLayout对齐/定位不起作用

[英]RelativeLayout alignment/positioning not working

I've had a look around, and not been able to find a fix to this problem. 我四处张望,却无法找到解决此问题的方法。 Yes it is something that has been asked before, but none of the solutions I've found has corrected my problem. 是的,以前已经有人问过这个问题,但是我发现的解决方案都没有解决我的问题。
So basically, my problem is that I need two views to align to the right of their container, one next to the other. 因此,基本上,我的问题是我需要两个视图以使其容器右侧对齐,一个视图彼此相邻。 I can't stop them from overlapping however. 但是,我不能阻止它们重叠。

RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams((int) (screenWidth*0.30), LayoutParams.WRAP_CONTENT);
param.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
newSpinner.setLayoutParams(param);
newSpinner.setId(-101);

param = new RelativeLayout.LayoutParams((int) (screenWidth*0.10), LayoutParams.WRAP_CONTENT);
param.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
param.addRule(RelativeLayout.LEFT_OF, newSpinner.getId());
newButton.setLayoutParams(param);

The above code results in this: http://i.imgur.com/o1GlZLV.png 上面的代码就是这样的: http : //i.imgur.com/o1GlZLV.png

param.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

While removing the above line results in this: http://i.imgur.com/25vlmrw.png 删除上述行会导致以下结果: http : //i.imgur.com/25vlmrw.png

So what's the fix? 那么解决方法是什么?

You are using the same object 'param' for both spinner and button. 您正在为微调器和按钮使用相同的对象“ param”。 Try using like param1 and param2 for each in the first place. 首先尝试分别使用like param1和param2。 Secondly try using 其次尝试使用

param.addRule(RelativeLayout.ALIGN_PARENT_LEFT); for one item and param.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 对于一项和param.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); for the other to avoid overlapping. 其他避免重叠。

Also try this solution. 也尝试此解决方案。 Create a relative layout with param.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 使用param.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);创建一个相对布局param.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); and then add spinner and button in this relative layout, use ALIGN_PARENT_RIGHT for one item and LEFT_OF for the other. 然后添加微调和按钮,在这个相对布局,使用ALIGN_PARENT_RIGHT一个项目和LEFT_OF为其他。 Also make sure to use the appropriate width of the items so that they should not overlap 另外,请确保使用适当的宽度,以免重叠

Remove: param.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 删除:param.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

From newButton. 从newButton。

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

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