代码之家  ›  专栏  ›  技术社区  ›  Nishant Shah

如何通过代码在RelativeLayout中定位按钮?

  •  5
  • Nishant Shah  · 技术社区  · 15 年前
    < RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
            < Button android:text="Previous" 
                android:layout_height="wrap_content" 
                android:id="@+id/MeasurePrev"           
                android:layout_alignParentBottom="true"
                android:layout_width="wrap_content">
            < / Button>
    < / RelativeLayout >
    

    有人能告诉我如何使用Java代码或在活动类中实现这一点吗? 我不知道如何设置android:layout_alignParentBottom=“真的”。 我想通过java代码实现整个视图。

    1 回复  |  直到 15 年前
        1
  •  16
  •   Sephy    15 年前

    下面是代码的动态等价物:

        RelativeLayout rl = new RelativeLayout(this);
        LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        rl.setLayoutParams(params);
        Button button = new Button(this);
        button.setText("Previous");
        LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        button.setLayoutParams(params1);
        rl.addView(button);