package com.gaodu;import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;public class RadioAct extends Activity {
private TextView text; public RadioAct() { } @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layear = new LinearLayout(this);
layear.setOrientation(LinearLayout.VERTICAL);
text = new TextView(this);
text.setText("请问你是?");
RadioGroup rdGroup = new RadioGroup(this);
rdGroup.setOrientation(LinearLayout.VERTICAL);
RadioButton rb1 = new RadioButton(this); rb1.setText("男");
RadioButton rb2 = new RadioButton(this);
rb2.setText("女");
rdGroup.addView(rb1, 1, ViewGroup.LayoutParams.FILL_PARENT);
rdGroup.addView(rb2, 2, ViewGroup.LayoutParams.FILL_PARENT);
rdGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == 1) { text.setText("男");
} else { text.setText("女");
} }
});
layear.addView(text, new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
layear.addView(rdGroup, new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
setContentView(layear);
}}
奇怪的是界面显示出了一行请问你是?,,而RadioGroup却没显示出来!

解决方案 »

  1.   

    我想应该是被挤到外面去了,,,你不加layear.addView(text, new LayoutParams(LayoutParams.WRAP_CONTENT, 
    LayoutParams.WRAP_CONTENT)); 试试
      

  2.   

    package com.gaodu;import android.app.Activity;
    import android.os.Bundle;
    import android.view.ViewGroup;
    import android.widget.LinearLayout;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.RadioGroup.LayoutParams;
    import android.widget.RadioGroup.OnCheckedChangeListener;
    import android.widget.TextView;public class RadioAct extends Activity {

    private TextView text; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LinearLayout layear = new LinearLayout(this);
    layear.setOrientation(LinearLayout.VERTICAL);
    text = new TextView(this);
    text.setText("请问你是?");
    RadioGroup rdGroup = new RadioGroup(this);
    rdGroup.setOrientation(LinearLayout.VERTICAL);
    RadioButton rb1 = new RadioButton(this); rb1.setText("男");
    RadioButton rb2 = new RadioButton(this);
    rb2.setText("女");
    rdGroup.addView(rb1, RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
    rdGroup.addView(rb2, RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
    rdGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) {
    if (checkedId == 1) { text.setText("男");
    } else { text.setText("女");
    } }
    });
    layear.addView(text, new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
    LayoutParams.WRAP_CONTENT));
    layear.addView(rdGroup, new LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
    LayoutParams.FILL_PARENT));
    setContentView(layear);

    }
    像这样改一下,rdGroup.addView(rb1, 1, ViewGroup.LayoutParams.FILL_PARENT); 的问题,1不是id而是宽度。
      

  3.   

    可能是你的TextView的Height设置成了fill_parent了将选项挤出去了