2012-02-28 25 views
1

我有一個活動,所有顯示元素動態添加。 Theres沒有xml的活力。以編程方式將項目放入活動中。 NO XML

的活動包括以下控件:

  1. 的RelativeLayout(所有的子視圖坐在佈局對象)
  2. 的TextView(頁面標題,坐鎮RelativeLayout的頂部)
  3. 滾動型(保存所有的數據控制滾動區)
  4. 的LinearLayout(佈局對象來保存活動按鈕)

我想知道如何定義ScrollView位於標題TextView下面,LinearLayout按鈕的上方,LinearLayout設置爲活動頁面底部的位置。我嘗試過使用RelativeLayout.LayoutParams進行設置規則,但似乎無法理解如何去做。任何幫助或教程的鏈接將apreciated

我已經包括了我的代碼,看看是否有人能幫助

// declare the items for display 
    RelativeLayout baseLayout = new RelativeLayout(this); 
    // add the customer name and number field. 
    // NOTE: We will always require this widget regardless of dialog design fields 
    tvCustomerNameNumber = new TextView(this); 
    tvCustomerNameNumber.setTextSize(20); 
    tvCustomerNameNumber.setText("Customer Name & Number"); 

    // build up the linear layout of controls 
    LinearLayout ll = new LinearLayout(this); 
    ll.setOrientation(LinearLayout.VERTICAL);  

    // Scroll view. 
    // NOTE: We will always need this widget to enable us to scroll the page 
    // if too many items are added for the display size 
    ScrollView sv = new ScrollView(this); 
    sv.addView(ll); 

    // buttons 
    LinearLayout buttons = new LinearLayout(this); 
    buttons.setOrientation(LinearLayout.HORIZONTAL); 

    // button edit 
    Button edit = new Button(this); 
    edit.setId(EDIT_BUTTON_ID); 

    // button save 
    Button save = new Button(this); 
    save.setId(SAVE_BUTTON_ID); 

    // button cancel 
    Button cancel = new Button(this); 
    cancel.setId(CANCEL_BUTTON_ID); 

    // add each button to the button layout 
    buttons.addView(edit); 
    buttons.addView(save); 
    buttons.addView(cancel); 

    // Scroll view Layout parameters 
    RelativeLayout.LayoutParams scrollParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
    scrollParams.addRule(RelativeLayout.BELOW, tvCustomerNameNumber.getId()); 
    scrollParams.addRule(RelativeLayout.ABOVE, buttons.getId()); 

    // buttons Layout parameters 
    RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    buttonParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
    buttonParams.addRule(RelativeLayout.BELOW, sv.getId()); 

    // add the customer name number field to the base layout 
    baseLayout.addView(tvCustomerNameNumber); 
    // add the scroll view to the base layout 
    baseLayout.addView(sv); //, scrollParams);  
    // add the buttons to the base layout 
    baseLayout.addView(buttons, buttonParams); 

    // set the content view 
    this.setContentView(baseLayout); 

回答

1

Deva已經回答了你的問題,但它聽起來像你可以定義一個xml佈局,如上所述,膨脹它並以編程方式動態填充它...也許佈局最初將包含一個空的LinearLayout,並且/或者沒有爲TextView設置文本,甚至可以將所有內容設置爲android:visibility =「gone」並在您添加/更新所有視圖時顯示它?

+0

最後,我用textview,scrollview和horizo​​ntalLINElayouts創建了一個簡單的xml文件,並動態地將我的組件添加到scrollview內的一個線性佈局 – 2012-02-28 15:18:04

1
+0

這幫了我很多,但我仍然努力讓scrollview坐在我的文本視圖和按鈕佈局之間。 似乎scrollview只是被放在活動FILL_PARENT,FILL_PARENT上,這樣它就會填充相關佈局,並忽略我設置的規則。 – 2012-02-28 14:21:43

+0

我將我的代碼添加到我的問題帖子中。它已被剝離與該問題無關的任何代碼 – 2012-02-28 14:30:14

相關問題