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

问题描述

我试图让一个单选按钮网格我的应用程序,我所学到的是,这是不可能使用普通的 RadioGroup中,因为它扩展的LinearLayout和如果您尝试安排单选按钮使用RelativeLayout的在 RadioGroup中 RadioGroup中不看按钮 RelativeLayout的

因此​​,为了解决这个问题我想打一个自定义的RadioGroup中扩展RelativeLayout的,而不是LinearLayout中。

我如何做到这一点?

更新:我没有你所说的话,但我有这些错误,我不知道如何在类文件修复:

 描述资源路径位置类型
RadioGroup_checkedButton不能得到解决或不是场RadioGrou prelative.java / BlockBall / src目录/ COM / stickfigs / blockball线81 Java问题
构造RelativeLayout.LayoutParams(整型,整型,浮点)是未定义RadioGrou prelative.java / BlockBall / src目录/ COM / stickfigs / blockball线265 Java问题
该方法setOnCheckedChangeWidgetListener(CompoundButton.OnCheckedChangeListener)是未定义的类型单选RadioGrou prelative.java / BlockBall / src目录/ COM / stickfigs / blockball线363 Java问题
该方法setOnCheckedChangeWidgetListener(空)是未定义的类型单选RadioGrou prelative.java / BlockBall / src目录/ COM / stickfigs / blockball线377 Java问题
垂直不能被解析为一个变量RadioGrou prelative.java / BlockBall / src目录/ COM / stickfigs / blockball线68 Java问题
Widget_CompountButton_RadioButton不能得到解决或不是场RadioGrou prelative.java / BlockBall / src目录/ COM / stickfigs / blockball线79 Java问题
 

解决方案

您需要从 RadioGroup中的源$ C ​​$ C href="http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/widget/RadioGroup.java;h=393346a314f7b1ad20c617c494904f599391c081;hb=HEAD"相对=nofollow>此处,与 RelativeLayout的的LinearLayout 所有条目>。

将此code一些XML文件在您的项目(通常它的名字是attr​​s.xml):

 <资源>
    <申报,设置样式名称=RadioGroup中>
        < attr指示名=机器人:checkedButton/>
    < /申报,设置样式>
< /资源>
 

替换 RadioGroup中的构造与这些:

 公共RadioGroup中(上下文的背景下){
    超(上下文);
    如果(!isInEditMode()){
        在里面();
    }
}

公共RadioGroup中(上下文的背景下,ATTRS的AttributeSet){
    超(背景下,ATTRS);
    如果(!isInEditMode()){
        TypedArray属性= context.obtainStyledAttributes(
                的attrs,R.styleable.RadioGroup,0,
                android.R.style.Widget_CompoundButton_RadioButton);

        int值= attributes.getResourceId(R.styleable.RadioGroup_checkedButton,
            View.NO_ID);
        如果(值!= View.NO_ID){
            mCheckedId =价值;
        }

        attributes.recycle();
        在里面();
    }
}
 

拆下的LayoutParams 内部类下面的构造:

 公开的LayoutParams(INT W,INT小时,浮initWeight){
    超(W,H,initWeight);
}
 

替换 setOnCheckedChangeWidgetListener所有出现的()与 setOnCheckedChangeListener()方法方法调用。 重要:在这种情况下,将无法从使用该部件一个code使用这种方法

有没有试过,但希望这会工作。

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
解决方案

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.

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

09-14 14:09