Я столкнулся со сценарием, в котором main.xml имеет свою основную иерархию, написанную на XML, но основные данные поступают с сервера, поэтому мне нужно составить макет во время выполнения.
Пожалуйста,взгляните на этот снимок экрана , чтобы лучше понять мой сценарий.
У меня будет Scroll View, а затем прямо под ним, Linear Layout.Я извлекаю этот макет в своем классе Java и создаю новый линейный макет, чтобы я мог создавать новые TextViews и кнопки, как я рисую в LinearLayout 2. Конечный результат далек от того, что я ожидал, и я, честно говоря, не уверен, как мне это сделатьthis.
Код для:
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);
Спасибо за ваше время!Фелипе