以下のように実装されたカスタム ダイアログに問題があります。
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setTitle("Title");
dialogBuilder.setMessage("Message");
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
// Set an EditText view to get user input
EditText input = new EditText(this);
input.setHeight(LayoutParams.WRAP_CONTENT);
input.setWidth(LayoutParams.MATCH_PARENT);
layout.addView(input);
TextView text = new TextView(this);
text.setText("show some text here");
text.setHeight(LayoutParams.WRAP_CONTENT);
text.setWidth(LayoutParams.MATCH_PARENT);
//text.setHeight(50);
//text.setWidth(150);
text.setVisibility(TextView.VISIBLE);
layout.addView(text);
Button btn = new Button(this);
btn.setText("Button");
btn.setHeight(LayoutParams.WRAP_CONTENT);
btn.setWidth(LayoutParams.WRAP_CONTENT);
layout.addView(btn);
dialogBuilder.setView(layout);
dialogBuilder.setPositiveButton("Ok", null);
AlertDialog myDialog = dialogBuilder.create();
myDialog.show();
問題は、TextView text
上記のコードのように幅と高さを設定すると、ダイアログに表示されないことです。プロパティ (2 つのコメント行) をハードコーディングした場合にのみ表示されますが、それは望ましい動作ではありません。
また、Button の幅は に設定されてWRAP_CONTENT
いますが、画面と同じ大きさです。
誰かが私がここで間違っていることを指摘してもらえますか?