2011-06-12 5 views
7

내 앱 라디오 버튼의 격자를 만들기 위해 노력하고는, 내가 배운 것은이가있는 LinearLayout를 확장하기 때문에 정기적으로 RadioGroup를 사용 불가능하다는 것이다 당신은 RadioButtons 사용 RelativeLayout의 내부 RadioGroupRadioGroup을 준비하려고하면 RelativeLayout 안에 Buttons이 표시되지 않습니다.RadioGroup 확장 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 
+0

는 [을 radioGroup의 사용없이 라디오 버튼에서 처리보세요. [1] [1] : http://stackoverflow.com/questions/6332042/setting-up-a-radiogroup-programmatically/ 7019673 # 7019673 – Matt

답변

8

당신은, here에서 RadioGroup의 소스 코드를 얻을 RelativeLayoutLinearLayout의 모든 항목을 교체해야합니다.

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

을 다음과 같은 생성자를 제거

프로젝트에서 일부 XML 파일에이 코드를 추가합니다 (일반적으로 이름이 attrs.xml이입니다) LayoutParams 내부 클래스에서 :

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

복제본 setOnCheckedChangeListener() 메서드를 사용하여 setOnCheckedChangeWidgetListener() 메서드 호출을 모두 발생시킵니다. 중요 :이 경우이 위젯을 사용하는 코드에서이 메서드를 사용할 수 없습니다.

시도해 보지 않았 으면 좋겠다.

+0

이것은 좋은 것처럼 들리지만 XML과 생성자에 대한 변경 사항은 무엇입니까? RelativeLayout을 사용하여 LinearLayout을 리플렉션하는 것만으로도 충분하다고 생각했는데, 틀렸습니까? –

+0

네, 틀렸어. 일부 내부 필드가'RadioGroup '의 생성자에서 사용되기 때문에 컴파일 만하지 않습니다. 그래서이 XML이 필요합니다. 'RadioGroup' 위젯의'android : checkedButton' 속성을 다시 정의 할 수 있습니다. 그리고 XML에서 값을 사용해야하기 때문에 생성자를 변경해야합니다. – Michael

+0

그래, 내가 말한대로, 새로운 클래스 파일에 몇 가지 오류가 있는데, 위의 질문에 추가했다 ... –

2

복사 here에서을 radioGroup에 대한 소스가로 변경 편집 LinearLayout 대신 RelativeLayout을 확장하십시오.