将无线电组添加到自定义对话框

时间:2012-11-20 19:33:21

标签: android dialog radio-button

我正在尝试动态单选按钮到自定义对话框,但无法在自定义对话框中添加它

 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.

任何指向此问题的指针都是适用的。

谢谢! 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>

1 个答案:

答案 0 :(得分:6)

查看您发布的XML,试试这个:

Context context=LocationActivity.this;
dialog=new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

View view = LayoutInflater.fromContext(context).inflate(R.layout.custom_dialog, null, false);
RadioGroup rg = (RadioGroup) view.findViewById(R.id.radiogroup);
RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(
                RadioGroup.LayoutParams.WRAP_CONTENT,
                RadioGroup.LayoutParams.WRAP_CONTENT);

 // add 5 radio buttons to the group
RadioButton rb;
for (int i = 0; i < 5; i++){
    rb = new RadioButton(context);
    rb.setText("item set" + i);
    rb.setId(i);
    rg.addView(rb, layoutParams);
}

dialog.setContentView(view);

我没看到你是如何改变你的Dialog布局的。所以我夸大了布局,添加了RadioButtons,然后将完成的布局传递给dialog

此外,由于linearMain只有一个孩子,因此您可以删除此LinearLayout并只使用radioGroup

相关问题