//* Linear Layout *
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setTag("linearLayout" + Integer.toString(id));
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setBackgroundResource(R.drawable.background_border_1);
linearLayout.setGravity(Gravity.CENTER_VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(300, 70);
layoutParams.setMargins(20, 10, 20, 10);
linearLayout.setWeightSum(10);
linearLayout.setLayoutParams(layoutParams);
Toast.makeText(getBaseContext(), Integer.toString((int)linearLayout.getWeightSum())+ ", " + Integer.toString(linearLayout.getHeight()) + ", " + Integer.toString(linearLayout.getWidth()), Toast.LENGTH_LONG).show();
//* Edit Text *
EditText editText = new EditText(this);
editText.setTag("editText" + Integer.toString(id));
layoutParams.width = LinearLayout.LayoutParams.WRAP_CONTENT;
layoutParams.height = 50;
layoutParams.setMargins(10, 0, 10, 0);
layoutParams.weight = 2;
editText.setEms(10);
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
editText.setLayoutParams(layoutParams);
editText.setGravity(Gravity.CENTER);
//* Text View *
TextView textView = new TextView(this);
textView.setTag("textView" + Integer.toString(id));
layoutParams.height = 50;
layoutParams.setMargins(10, 0, 10, 0);
layoutParams.weight = 4;
textView.setLayoutParams(layoutParams);
textView.setGravity(Gravity.CENTER);
textView.setText("Credit Hours");
//* Spinner *
Spinner spinner = new Spinner(this);
spinner.setTag("spinner" + Integer.toString(id));
layoutParams.width = 50;
layoutParams.height = LinearLayout.LayoutParams.MATCH_PARENT;
spinner.setGravity(Gravity.FILL_VERTICAL | Gravity.CENTER | Gravity.CENTER_VERTICAL);
layoutParams.setMargins(10, 0, 10, 0);
layoutParams.weight = 4;
spinner.setLayoutParams(layoutParams);
linearLayout.addView(editText);
linearLayout.addView(textView);
linearLayout.addView(spinner);
llSubjectsContainer = (LinearLayout) findViewById(R.id.llSubjectsContainer);
llSubjectsContainer.addView(linearLayout);
これは私のコードですが、動作するはずです。しかし、linearLayoutの高さも幅もこのコードからもマージンからも設定されています。私はすでに何時間も費やしてきましたが、実用的な解決策は見つけられません。アンドロイドの線形レイアウトの高さと幅が設定されていません
このコードをonCreate()内に置いてもよろしいですか? –
いいえボタンのクリックでこれを動作させたい場合 – Faiq