2010-07-27 83 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