7

我正在尝试为我的应用程序制作一个单选按钮网格,我了解到这是不可能使用常规的RadioGroup,因为它扩展了 LinearLayout 并且如果您尝试安排RadioButtons使用 RelativeLayout INSIDE 则RadioGroupRadioGroup不到Buttons内部RelativeLayout. _

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

我该怎么做呢?

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

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
4

3 回答 3

9

您需要从此处RadioGroup获取的源代码,将 的所有条目替换为。LinearLayoutRelativeLayout

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

<resources>
    <declare-styleable name="RadioGroup">
        <attr name="android:checkedButton" />
    </declare-styleable>
</resources>

用这些替换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();
    }
}

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

public LayoutParams(int w, int h, float initWeight) {
    super(w, h, initWeight);
}

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

还没有尝试过,但希望这会奏效。

于 2011-06-12T06:23:40.213 回答
2

从此处复制 RadioGroup 的源并对其进行编辑以将其更改为扩展 RelativeLayout 而不是 LinearLayout。

于 2011-06-12T06:11:59.573 回答
0

这超出了主题,但如果你想做同样的事情,ConstraintLayout那么代码可以在 Github上找到

您可以导入库并使用小部件ConstraintRadioGroup

于 2020-05-20T17:31:08.530 回答