RadioGroup 扩展relativelayout?

发布于 2024-11-15 04:30:12 字数 1444 浏览 1 评论 0 原文

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

因此,为了解决这个问题,我想创建一个扩展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

I'm trying to make a grid of radio buttons for my app, what I have learned is that this isn't possible using regular RadioGroup because it extends LinearLayout and if you try to arrange the RadioButtons using RelativeLayout INSIDE the RadioGroup the RadioGroup doesn't see the Buttons inside the RelativeLayout.

So in order to fix this I want to make a custom RadioGroup that extends RelativeLayout instead of LinearLayout.

How do I do this?

UPDATE: I did what you said but I have these errors I don't know how to fix in the class file:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清眉祭 2024-11-22 04:30:12

您需要从 RadioGroup 的源代码href="http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/widget/RadioGroup.java;h=393346a314f7b1ad20c617c494904f599391c081;hb= HEAD" rel="noreferrer">此处,替换所有条目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() 方法。 重要:在这种情况下,将无法在使用此小部件的代码中使用此方法。

还没有尝试过,但希望这会起作用。

You need to get the RadioGroup's source code from here, replace all entries of LinearLayout with RelativeLayout.

Add this code to some xml file in your project (usually its name is attrs.xml):

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

Replace RadioGroup's constructors with these:

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();
    }
}

Remove the following constructor from the LayoutParams inner class:

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

Replace all occurrences of setOnCheckedChangeWidgetListener() method calls with the setOnCheckedChangeListener() method. IMPORTANT: In this case it won't be possible to use this method from a code that uses this widget.

Haven't tried this but hope this will work.

温折酒 2024-11-22 04:30:12

这里并编辑它以将其更改为扩展RelativeLayout而不是LinearLayout。

Copy the source for RadioGroup from here and edit it to change it to extend RelativeLayout instead of LinearLayout.

浮世清欢 2024-11-22 04:30:12

这超出了主题,但如果您想使用 ConstraintLayout 做同样的事情,那么代码是 Github 上提供

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

This is out of topic, but in case if you wanna do the same thing with ConstraintLayout then the code is available at Github

You can import the library and use the widget ConstraintRadioGroup

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文