3

告诉我如何使用线性布局以编程方式将位置分配给 android 中的按钮。默认情况下,它占据屏幕的极左位置。我也想使用线性布局来做到这一点,请不要建议我使用相对布局。以下是我的代码

buttons_and_edittext=new LinearLayout(HelloAugmentedWorldActivity.this);
buttons_and_edittext = (LinearLayout)findViewById(R.id.linearLayout1);
buttons_and_edittextParameters = new LinearLayout.LayoutParams(120, 150);

button3 = new Button(this);
button3.setText("log");

buttons_and_edittext.addView(button3,      
buttons_and_edittextParameters);

任何帮助将不胜感激谢谢

4

2 回答 2

3

Please refer below link for add buttons into relative layout at fix position.

Android User Interface Design: Relative Layout

And Use Below Code also for that.

Button but1 = new Button(this);  
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
params2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);  
but1.setLayoutParams(params2);  
but1.setText("Press Here!");  
// give the button an id that we know  
but1.setId(1001);
layout1.addView(but1);
于 2012-07-04T10:00:36.570 回答
0

很长一段时间后,我得到了一个完美的解决方案,通过执行这些步骤,在 android studio 中借助相关布局参数以编程方式设置按钮和图像位置 - 请查看此链接

于 2021-01-09T11:19:12.507 回答