有关以编程方式创建布局的最佳方法的提示

时间:2012-02-06 09:18:18

标签: android android-layout

我正面临一个场景,我的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);

感谢您的时间! 菲利普

1 个答案:

答案 0 :(得分:0)

我认为更好的方法是使用ListView 在那里,您可以根据需要定义每一行。您也可以在运行时添加或删除行 当您想在运行时向其添加元素时,ScrollView很容易折叠。