android - 以编程方式将位置分配给android中的按钮

标签 android button

告诉我如何使用线性布局以编程方式为 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);

任何帮助将不胜感激

最佳答案

请引用以下链接将按钮添加到固定位置的相对布局。

Android User Interface Design: Relative Layout

也为此使用下面的代码。

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);

关于android - 以编程方式将位置分配给android中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11326384/

相关文章:

jquery - Angular 和 Typescript 上的 ToggleClass ('active' )

android - 如何在 Google Play Console 和 App Store Connect 中为单个应用配置多个自动续订订阅

android - 使用Service进行操作——异步任务

java - 安卓(Java): Getting GPS location accuracy

javascript - 多个列表和按钮,但文本输入未保存在正确的列表中

javascript - 如何通过单击按钮关闭 Fancybox?

android - 关于gradle中任务执行的顺序

Android material 对齐字体基线到 4dp 网格

Android Material float Action 按钮展开菜单效果

c# - 在 WP 中点击控件时不要隐藏软键盘