RadioGroup 扩展RelativeLayout? [英] RadioGroup extending RelativeLayout?

查看:34
本文介绍了RadioGroup 扩展RelativeLayout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

我该怎么做?

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

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

推荐答案

你需要从这里, 将 LinearLayout 的所有条目替换为 RelativeLayout.

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

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

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>

用这些替换 RadioGroup 的构造函数:

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

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

Remove the following constructor from the LayoutParams inner class:

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

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

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.

这篇关于RadioGroup 扩展RelativeLayout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆