我可以用android编程设置小部件的位置吗

我可以用android编程设置小部件的位置吗,android,Android,我可以用android编程来设置小部件的位置,而不是xmlcode。例如: 像这样。是否有方法代替上述xml代码 是的,例如,你可以 Button button1 = new Button(R.id.buttonn); Button button2 = new Button(R.id.buttonn2); button1.setX(button2.getX); 还有更多的函数,如setX,如果你需要什么特别的东西,只要问一下:)是的,你可以,例如 Button button1 = new B

我可以用android编程来设置小部件的位置,而不是
xml
code。例如:


像这样。是否有方法代替上述
xml
代码

是的,例如,你可以

Button button1 = new Button(R.id.buttonn);
Button button2 = new Button(R.id.buttonn2);
button1.setX(button2.getX);

还有更多的函数,如
setX
,如果你需要什么特别的东西,只要问一下:)

是的,你可以,例如

Button button1 = new Button(R.id.buttonn);
Button button2 = new Button(R.id.buttonn2);
button1.setX(button2.getX);

还有更多类似于
setX
的功能,如果您需要什么特别的东西,只需问一下:)

是的,您也可以通过代码设置所有带圆圈的属性。例如,此代码将按钮1与其父项(RelativeLayout)左侧对齐,并将按钮2放置在按钮1左侧

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
button1.setLayoutParams(params);

params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.RIGHT_OF, button1.getId());
button2.setLayoutParams(params);
记住

  • 布局与顶部对齐
  • 布局图\u对齐结束
  • 布局与底部对齐
  • 布局图左对齐
  • 布局\u开始对齐
  • 布局图
  • 上面的布局图
  • 下面的布局图
  • 布局\u中心项目
  • 平面布置图
仅当您将属性设置为的视图位于RelativeLayout内时,此选项才起作用

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
button1.setLayoutParams(params);

params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.RIGHT_OF, button1.getId());
button2.setLayoutParams(params);

原版,

是您也可以通过代码设置所有带圆圈的属性。例如,此代码将按钮1与其父项(RelativeLayout)左侧对齐,并将按钮2放置在按钮1左侧

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
button1.setLayoutParams(params);

params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.RIGHT_OF, button1.getId());
button2.setLayoutParams(params);
记住

  • 布局与顶部对齐
  • 布局图\u对齐结束
  • 布局与底部对齐
  • 布局图左对齐
  • 布局\u开始对齐
  • 布局图
  • 上面的布局图
  • 下面的布局图
  • 布局\u中心项目
  • 平面布置图
仅当您将属性设置为的视图位于RelativeLayout内时,此选项才起作用

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
button1.setLayoutParams(params);

params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.RIGHT_OF, button1.getId());
button2.setLayoutParams(params);
原件,

副本