如何解决RadioGroup扩展了RelativeLayout吗?
| 我正在尝试为我的应用程序制作一个单选按钮网格,据我了解到,使用常规RadioGroup
是不可能的,因为它扩展了LinearLayout,并且如果您尝试使用RelativeLayout将RadioButtons
布置在RadioGroup
ѭ0内在RelativeLayout
中看不到Buttons
。
因此,为了解决此问题,我想制作一个自定义的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
解决方法
您需要从此处获取
RadioGroup
的源代码,并将LinearLayout
的所有条目替换为RelativeLayout
。
将此代码添加到项目中的某些xml文件中(通常其名称为attrs.xml):
<resources>
<declare-styleable name=\"RadioGroup\">
<attr name=\"android:checkedButton\" />
</declare-styleable>
</resources>
将0的构造函数替换为:
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,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()
方法。重要说明:在这种情况下,将无法从使用此小部件的代码中使用此方法。
还没有尝试过,但希望能成功。
, 从此处复制RadioGroup的源,并对其进行编辑以进行更改以扩展RelativeLayout而不是LinearLayout。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。