以编程方式创建布局的最佳方式的提示
问题描述:
我面临一个场景,其中我的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
ListView中的一行可以包含TextView,Button和Image吗?因为我能找到的每一个例子都只是在这一行中添加纯文本。 – 2012-02-06 09:56:26
当然可以。您的ListView适配器中的getView方法可以返回您希望它返回的任何布局。有一个很好的例子,说明如何从ListView适配器开始:http://www.mkyong.com/android/android-listview-example/ – Thommy 2012-02-06 10:38:32
感谢您的帮助! – 2012-02-06 15:41:56