【问题标题】:Android Button Position Programmatically以编程方式的Android按钮位置
【发布时间】:2011-03-14 18:47:53
【问题描述】:

我的应用程序中有一个按钮。我想以编程方式改变它的位置。我在 XML 中创建了一个按钮,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<Button android:text="@+id/Button01" 
        android:id="@+id/Button01" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_marginLeft="50px"
        android:layout_marginTop="10px"
        >
</Button>
</LinearLayout>

假设我想将按钮的位置设置为距左侧 100px(如 layout_marginLeft="100px")。我怎样才能以编程方式做到这一点?请帮我解决问题。

【问题讨论】:

  • 填充和边距不一样...这是线程是您正在寻找的stackoverflow.com/questions/2481455/…
  • 让我进入正题...实际上我的布局包含 9 个按钮...所以根据条件有些按钮可见和不可见..所以我想对齐(向左或向右移动)它以编程方式...请帮助我
  • 看看这个答案。它清楚地显示了如何在按钮上设置边距。 stackoverflow.com/a/4594374/525541

标签: android android-widget


【解决方案1】:

卢克,使用

RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

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

myButton.setLayoutParams(rel_btn);

【讨论】:

    【解决方案2】:

    您需要获取视图并转换为 Java 对象,然后在其上调用setPadding

    这样的事情会解决的

    Button myBtn;
    myBtn = (Button) findViewById(R.id.Button01);
    myBtn.setPadding(0,100,0,0);
    

    在这里阅读更多: https://developer.android.com/reference/android/view/ViewGroup.MarginLayoutParams.html

    【讨论】:

    • 它不工作..在 setMargins 函数下显示红线(错误)。我正在使用 Android 1.5
    • 我也试过 btnTest.setPadding(100,0,0,0);但它使按钮宽度为 100,文本将显示在右侧。但仍然无法正常工作
    • 是的...如果我将“setPadding(100,0,0,0) 设置为布局,如下所示:LinearLayout l1 = (LinearLayout) findViewById(R.id.LinearLayout01); l1 .setPadding(120, 0, 0, 0); 非常感谢...@Pentium
    【解决方案3】:

    您不能将setMargin() 与 btn 一起使用,将其与 LayoutParams 一起使用,然后再使用 btn.setLayoutParams(params);

    【讨论】:

      【解决方案4】:

      使用Kotlin,您可以使用以下内容:

      class MainActivity : AppCompatActivity() {
      
          override fun onCreate(savedInstanceState: Bundle?) {
              super.onCreate(savedInstanceState)
              setContentView(R.layout.activity_main)
      
              val btnTag = Button(this).apply {
                  text = "Done"
                  setOnClickListener { finish() }
              }
      
              // btnTag.setOnClickListener { this.finish() }
      
              val lp = RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                      LayoutParams.WRAP_CONTENT).apply {
                  addRule(RelativeLayout.ALIGN_PARENT_BOTTOM)
                  addRule(RelativeLayout.ALIGN_PARENT_RIGHT)
              }
      
              main_layout.addView(btnTag, lp)
      }
      

      注意:布局应为RelativeLayout

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-04
        • 1970-01-01
        • 1970-01-01
        • 2015-01-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多