2010-07-27 63 views
5
< 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 =“true”。 我想通过java代码实现整个视图。如何通过代码在RelativeLayout中定位Button?

回答

16

这里是你的代码的动态等效:

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

它可以正常使用。谢谢Sephy – 2010-07-28 09:20:20

+5

如果有人需要在你的代码中设置'android:layout_alignParentBottom =“false”''params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,0);' – bancer 2012-04-05 03:28:18