2012-02-06 183 views
0

我面臨一個場景,其中我的main.xml的主要層次結構用XML編寫,但主數據來自服務器,所以我需要在運行時組合佈局。以編程方式創建佈局的最佳方式的提示

請看看這screenshot以更好地瞭解我的情況。

我將有一個滾動視圖,然後在它下面,一個線性佈局。我在我的java類中檢索這個佈局並創建一個新的線性佈局,這樣我就可以創建新的TextViews和Buttons,就像我在LinearLayout 2中繪製的那樣。最終結果與我預期的並不相符,而且我確實不知道該如何完成這個。

的代碼是:

LinearLayout ll = (LinearLayout) findViewById(R.id.linearLayoutOffers); //this is the LinearLayout2 in SS 

    //Creating the container that will have one business. 
    LinearLayout containerMain = new LinearLayout(this); 
    containerMain.setOrientation(LinearLayout.VERTICAL); 
    containerMain.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
    containerMain.setGravity(Gravity.CENTER); 

    LinearLayout container1 = new LinearLayout(this); 
    container1.setOrientation(LinearLayout.HORIZONTAL); 
    container1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 
    container1.setGravity(Gravity.CENTER); 
    container1.setWeightSum(2); 

    LinearLayout container2 = new LinearLayout(this); 
    container2.setOrientation(LinearLayout.HORIZONTAL); 
    container2.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 
    container2.setGravity(Gravity.CENTER); 
    container2.setWeightSum(1); 

    LinearLayout container3 = new LinearLayout(this); 
    container3.setOrientation(LinearLayout.HORIZONTAL); 
    container3.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 
    container3.setGravity(Gravity.CENTER); 
    container3.setWeightSum(1); 

    TextView offerTitle = new TextView(this); 
    offerTitle.setText(pOfferTitle); 
    offerTitle.setTextSize(14); 
    //offerTitle.setPadding(10, 0, 10, 0); 
    offerTitle.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1)); 

    Button redeemButton = new Button(this); 
    redeemButton.setText("Usar"); 
    redeemButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1)); 
    //redeemButton.setPadding(10, 0, 10, 0); 
    redeemButton.setTextSize(14); 

    container1.addView(offerTitle); 
    container1.addView(redeemButton); 

    TextView mAddress = new TextView(this); 
    mAddress.setText(pAddress); 
    mAddress.setTextSize(14); 
    //mAddress.setPadding(10, 0, 10, 0); 
    mAddress.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1)); 

    container2.addView(mAddress); 

    TextView expiryDate = new TextView(this); 
    expiryDate.setText(pExpiryDate); 
    expiryDate.setTextSize(14); 
    //expiryDate.setPadding(10, 0, 10, 0); 
    offerTitle.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.6f)); 

    Button detailsButton = new Button(this); 
    detailsButton.setText("Detalhes"); 
    detailsButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.4f)); 
    //detailsButton.setPadding(30, 0, 0, 0); 
    detailsButton.setTextSize(14); 

    container3.addView(expiryDate); 
    container3.addView(detailsButton); 

    containerMain.addView(container1); 
    containerMain.addView(container2); 
    containerMain.addView(container3); 

    ll.addView(containerMain); 

感謝您的時間! Felipe

回答

0

我認爲更好的方法是使用ListView
在那裏你可以像你想要的那樣定義每一行。您也可以在運行時添加或刪除行。
當您想要在運行時向其添加元素時,ScrollView會輕鬆摺疊。

+0

ListView中的一行可以包含TextView,Button和Image嗎?因爲我能找到的每一個例子都只是在這一行中添加純文本。 – 2012-02-06 09:56:26

+0

當然可以。您的ListView適配器中的getView方法可以返回您希望它返回的任何佈局。有一個很好的例子,說明如何從ListView適配器開始:http://www.mkyong.com/android/android-listview-example/ – Thommy 2012-02-06 10:38:32

+0

感謝您的幫助! – 2012-02-06 15:41:56