【问题标题】:programmatically assign position to a button in android以编程方式将位置分配给android中的按钮
【发布时间】:2012-07-04 19:09:26
【问题描述】:

告诉我如何使用线性布局以编程方式将位置分配给 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);

任何帮助将不胜感激,谢谢

【问题讨论】:

  • 非常感谢。我只需要使用buttons_and_edittextParameters.setMargins(30, 20, 30, 0);
  • 因为你建议我一个网站,我可以得到关于使用相对布局的好教程,我总结了使用相对布局感到不舒服的地方,因为我是 android 新手。
  • @user1278731 请看我的回答。

标签: android button


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    请参考以下链接在固定位置将按钮添加到相对布局中。

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

    【讨论】:

      猜你喜欢
      • 2011-03-14
      • 1970-01-01
      • 2018-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-09
      • 1970-01-01
      相关资源
      最近更新 更多