简体   繁体   English

在linearlayout中以编程方式设置android中按钮的位置

[英]To set the position of button in android programmatically in linearlayout

I have one button in an linear Layout .我在线性布局中有一个按钮。 From there I am able to set the (x,y) position of the button.从那里我可以设置按钮的 (x,y) 位置。 How can I get and set the (x,y) coordinates of the button by java program ?如何通过java程序获取和设置按钮的(x,y)坐标?

u cant do this with linear layout use Relative layout for positioning the component to absolute position inside parent view.你不能用线性布局来做到这一点,使用相对布局将组件定位到父视图内的绝对位置。

https://stackoverflow.com/a/3295056/1405008 https://stackoverflow.com/a/3295056/1405008

refer the above answer for more information.有关更多信息,请参阅上述答案。

LinearLayout.LayoutParams btn = new LinearLayout.LayoutParams(
                    R.id.layout_name);

btn.leftMargin = myXPosition;
btn.topMargin = myYPosition;
btn.width = buttonW;
btn.height = buttonH;

myButton.setLayoutParams(btn);

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

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