3

我正在尝试制作均衡器 RadioGroup。而且我想为预设动态添加单选按钮。

这是我的布局 XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RadioGroup
        android:id="@+id/rgEqualizerPreset"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    </RadioGroup>

</LinearLayout>

这是Java代码:

RadioGroup rgEqualizer;
List<RadioButton> radioButtonList;
MediaPlayer mPlayer;
Equalizer mEqualizer;
LinearLayout.LayoutParams layoutParams;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.equalizer);
    rgEqualizer = (RadioGroup) findViewById(R.id.rgEqualizerPreset);

    mPlayer = new MediaPlayer();
    mEqualizer = new Equalizer(0, mPlayer.getAudioSessionId());
    rgEqualizer = new RadioGroup(this);
    radioButtonList = new ArrayList<RadioButton>();

    fillRadioGroupWithRadioButtons();
    rgEqualizer.setOnCheckedChangeListener(this);
}

private void fillRadioGroupWithRadioButtons() {

    Short noPresets = mEqualizer.getNumberOfPresets();
    int i = 0;
    while (i < noPresets) {
        RadioButton rb = new RadioButton(this);
        rb.setText(mEqualizer.getPresetName((short) i));
        layoutParams = new RadioGroup.LayoutParams(
                RadioGroup.LayoutParams.FILL_PARENT,
                RadioGroup.LayoutParams.FILL_PARENT);
        rgEqualizer.addView(rb, layoutParams);
        i++;
    }
}

@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
    // TODO Auto-generated method stub

}

当此活动打开时,仅显示空白布局屏幕。我错过了什么?

谢谢

4

1 回答 1

2

问题是 rgEqualizer 实际上不是您布局中的 RadioGroup。你可能的意思是:

rgEqualizer = (RadioGroup) this.findViewById(R.id.rgEqualizerPreset);

代替:

rgEqualizer = new RadioGroup(this);

所以你的单选按钮被添加得很好,它们与你的布局无关。我感觉到你的痛苦。

于 2012-05-24T16:57:22.523 回答