简体   繁体   English

如何用约束布局交换两个按钮的位置?

[英]How to swap the position of two buttons with constraint layout?

I'm am trying to swap the position of two buttons around when something in app happens, however I do not believe that it is working because the layout is causing the buttons to stay as they are set to be a certain amount of "dp" away from each other so that I can have them where I want on the screen. 我正在尝试在应用程序中发生某些事情时交换两个按钮的位置,但是我不认为这是可行的,因为布局会导致按钮停留,因为它们被设置为一定量的“ dp”彼此远离,以便我可以将它们放在屏幕上想要的位置。 Is there a way to fix this, such as: Swapping the layout properties of each button or using different layout methods to have the buttons where I want on the screen. 是否有解决此问题的方法,例如:交换每个按钮的布局属性,或使用不同的布局方法将按钮放置在屏幕上所需的位置。 The code I am using to swap the buttons around is: 我用来交换按钮的代码是:

float posX = answer1.getX();
float posY = answer1.getY();
float pos2X = answer2.getX();
float pos2Y = answer2.getY();
answer2.setX(posX);
answer2.setY(posY);
answer1.setX(pos2X);
answer1.setY(pos2Y);

Thanks for your help :) 谢谢你的帮助 :)

Just a guess that might be helpful. 只是一个猜测可能会有所帮助。 Have you considered swapping the buttons themselves instead of touching their positions? 您是否考虑过自己更换按钮而不是触摸它们的位置?

Button temp1 = answer1;
Button temp2 = answer2;

answer1 = temp2;
answer2 = temp1;

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

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