【问题标题】:RadioGroup extending RelativeLayout?RadioGroup 扩展RelativeLayout?
【发布时间】:2011-09-13 06:57:25
【问题描述】:

我正在尝试为我的应用程序制作一个单选按钮网格,我了解到使用常规 RadioGroup 是不可能的,因为它扩展了 LinearLayout 并且如果您尝试使用 RelativeLayout 来排列 RadioButtonsRadioGroup 内部,RadioGroup 看不到ButtonsRelativeLayout 内部。

所以为了解决这个问题,我想制作一个自定义 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

【问题讨论】:

标签: java android xml radio-button radio-group


【解决方案1】:

您需要从here获取RadioGroup的源代码,将LinearLayout的所有条目替换为RelativeLayout

将此代码添加到项目中的某个 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() 方法。 重要提示:在这种情况下,将无法从使用此小部件的代码中使用此方法。

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

【讨论】:

  • 这听起来不错,但是 XML 和对构造函数的更改是为了什么?我只是想用 RelativeLayout 替换 LinearLayout 就足够了,我错了吗?
  • 是的,你错了。它不会只是编译,因为RadioGroup's 构造函数中使用了一些内部字段。这就是为什么你需要这个 xml。它允许您为RadioGroup 小部件重新定义android:checkedButton 属性。并且必须对构造函数进行更改,因为您需要使用 xml 中的值。
  • 好吧,我照你说的做了,不过我在新的类文件中有一些错误,将它们添加到上面的问题中......
  • 1) Attrs.xml 文件必须有资源作为根标签。将其添加到答案中。 2)只需删除此构造函数。 3,4) 那是个问题。此方法是包私有的,因此在您的代码中不可用。您可以使用“setOnCheckedChangeListener”,但在这种情况下,您将无法在代码的其他部分使用它。 5) 从构造函数中删除行setOrientation(VERTICAL)。 6) 那只是 Compound 一词中的一种类型。在答案中修复它。
  • 注意:完成这项工作后,我添加了一个项目,其中包含对 github 的更改:github.com/valheru7/RelativeRadioGroup
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    这不是主题,但如果你想用ConstraintLayout 做同样的事情,那么代码是available at Github

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-20
      • 1970-01-01
      • 2011-09-16
      相关资源
      最近更新 更多