RadioGroup扩展了RelativeLayout?

Kyl*_* V. 7 java xml android radio-group radio-button

我试图让单选按钮的网格,我的应用程序,我学到的是,这使用常规是不可能的RadioGroup,因为它扩展的LinearLayout,如果你试图安排RadioButtons使用的RelativeLayout里面RadioGroupRadioGroup看不到Buttons里面这个RelativeLayout.

所以为了解决这个问题,我想制作一个扩展RelativeLayout而不是LinearLayout的自定义RadioGroup.

我该怎么做呢?

更新:我做了你说的但我有这些错误我不知道如何修复类文件:

Description Resource    Path    Location    Type
RadioGroup_checkedButton cannot be resolved or is not a field   RadioGroupRelative.java /BlockBall/src/com/stickfigs/blockball  line 81 Java Problem
The constructor RelativeLayout.LayoutParams(int, int, float) is undefined   RadioGroupRelative.java /BlockBall/src/com/stickfigs/blockball  line 265    Java Problem
The method setOnCheckedChangeWidgetListener(CompoundButton.OnCheckedChangeListener) is undefined for the type RadioButton   RadioGroupRelative.java /BlockBall/src/com/stickfigs/blockball  line 363    Java Problem
The method setOnCheckedChangeWidgetListener(null) is undefined for the type RadioButton RadioGroupRelative.java /BlockBall/src/com/stickfigs/blockball  line 377    Java Problem
VERTICAL cannot be resolved to a variable   RadioGroupRelative.java /BlockBall/src/com/stickfigs/blockball  line 68 Java Problem
Widget_CompountButton_RadioButton cannot be resolved or is not a field  RadioGroupRelative.java /BlockBall/src/com/stickfigs/blockball  line 79 Java Problem
Run Code Online (Sandbox Code Playgroud)

Mic*_*ael 8

您需要获得RadioGroup来自的源代码在这里,更换的所有条目LinearLayoutRelativeLayout.

将此代码添加到项目中的某个xml文件中(通常名称为attrs.xml):

<resources>
    <declare-styleable name="RadioGroup">
        <attr name="android:checkedButton" />
    </declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)

RadioGroup用这些替换构造函数:

public RadioGroup(Context context) {
    super(context);
    if (!isInEditMode()) {
        init();
    }
}

public RadioGroup(Context context, AttributeSet attrs) {
    super(context, attrs);
    if (!isInEditMode()) {
        TypedArray attributes = context.obtainStyledAttributes(
                attrs, R.styleable.RadioGroup, 0,
                android.R.style.Widget_CompoundButton_RadioButton);

        int value = attributes.getResourceId(R.styleable.RadioGroup_checkedButton,
            View.NO_ID);
        if (value != View.NO_ID) {
            mCheckedId = value;
        }

        attributes.recycle();
        init();
    }
}
Run Code Online (Sandbox Code Playgroud)

LayoutParams内部类中删除以下构造函数:

public LayoutParams(int w, int h, float initWeight) {
    super(w, h, initWeight);
}
Run Code Online (Sandbox Code Playgroud)

setOnCheckedChangeWidgetListener()用该setOnCheckedChangeListener()方法替换所有出现的方法调用.重要说明:在这种情况下,将无法从使用此窗口小部件的代码中使用此方法.

没试过,但希望这会奏效.