我試圖動態單選按鈕,自定義對話框,但無法得到它在我的自定義對話框添加一個單選按鈕組到定製對話框
final RadioButton[] rb = new RadioButton[5];
RadioGroup rg = new RadioGroup(this);
rg.setOrientation(RadioGroup.VERTICAL);
// layout params to use when adding each radio button
LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
LinearLayout ll2 = (LinearLayout) findViewById(R.id.linearMain);
// add 5 radio buttons to the group
for (int i = 0; i < 5; i++){
rb[i] = new RadioButton(this);
String label = "item set" + i;
rb[i].setText(label);
rb[i].setId(i);
rg.addView(rb[i],layoutParams);
}
ll2.addView(rg);
--------------------------------------------------------------------
Context context=LocationActivity.this;
dialog=new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog);
----------------------------------------------------------------------
Linearlayout2 is defined in custom_dialog.xml file. I must be doing something wrong but not able to figure it out. Other widgets are getting displayed except radiogroup buttons.
任何指向這一問題appreicated增加。
謝謝! SWZ
CUSTOM_DIALOG文件,如下:
它有2個TextView的部件並申報了RadioGroup中。我可以將相同的佈局成功添加到活動屏幕,但不能添加到自定義對話框。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/linlayoutBase"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="5dip"
android:text="Longitute: "
android:textSize="20dip" >
</TextView>
<TextView
android:id="@+id/TextView04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="unknown"
android:textSize="20dip" >
</TextView>
</LinearLayout>
<LinearLayout
android:id="@+id/linearMain"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
</RadioGroup>
</LinearLayout>
</LinearLayout>
你可以添加'R.layout.custom_dialog'文件嗎? – Luksprog
您是否收到錯誤或者他們沒有出現?另外,我看到你說'Linearlayout2'在'custom_dialog'佈局中,但是你調用id'linearDialog' – jnthnjns
在那個XML中沒有帶有'@ + id/linearDialog'的ID的LinearLayout ...你是否得到了一個'NullPointerException'? – Sam