0
私のmain.xmlにはメイン階層がXMLで書かれていますが、メインデータはサーバーから来ているので、実行時にレイアウトを作成する必要があります。レイアウトをプログラムで作成する最善の方法のヒント
私のシナリオをよく理解するには、screenshotをご覧ください。
私はスクロールビューを持っていて、そのすぐ下にリニアレイアウトがあります。私のJavaクラスでこのレイアウトを取得し、新しい線形レイアウトを作成して、LinearLayout 2の中に描画するような新しいTextViewとButtonを作成できるようにします。最終結果は私が期待していたものではありません。この。
コードについては、次のとおりです。お時間を
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);
ありがとう! フェリペ
ListView内の行にTextView、Button、Imageが含まれていますか?私が見つけることができるたびに、この行にプレーンテキストを追加するだけです。 –
もちろんです。 ListView-AdapterのgetViewメソッドは、返すレイアウトを返すことができます。リストビューアダプターの使い方の良い例があります:http://www.mkyong.com/android/android-listview-example/ – Thommy
助けてくれてありがとう! –