7

私は自分のアプリのラジオボタンのグリッドを作成しようとしていますが、RadioGroupLinearLayoutを拡張するため、通常の使用ではこれが不可能であり、 RadioButtonsRelativeLayoutを内部に配置しようとすると、内部が表示さRadioGroupRadioGroupないことを学びました。 。Buttons_RelativeLayout

したがって、これを修正するために、LinearLayoutの代わりにRelativeLayoutを拡張するカスタム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
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のソースをコピーし、それを編集して、LinearLayoutではなくRelativeLayoutを拡張するように変更します。

于 2011-06-12T06:11:59.573 に答える
0

これはトピック外ですが、同じことをしたい場合はConstraintLayout、コードをGithubで入手できます。

ライブラリをインポートしてウィジェットを使用できますConstraintRadioGroup

于 2020-05-20T17:31:08.530 に答える