1

我有一個ConstraintLayout作爲根佈局,這很好。
但是我現在有一個RadioGroup,我必須在其中創建兩列RadioButton。 由於ConstraintLayout是關於擺脫嵌套佈局,我認爲將這些RadioButtons放在RadioGroup中並將它們恰當地放置是可以的。
原來有一個ConstraintLayout作爲根佈局,包含RadioGroup,似乎沒有改變任何東西。
但也許我錯了。ConstraintLayout,RadioGroup和兩列RadioButton

你們如何實現在一個RadioGroup內的兩行RadioButtons,它在一個ConstraintLayout內?

乾杯

回答

2

View■找用自己的直接父的佈局屬性。例如,您無法使用RadioButtonlayout_constraint,因爲直接父項是RadioGroupRadioGroup不知道如何解釋這些屬性。

RadioGroup延伸LinearLayout,所以你可以用一個單一的RadioGroup做的最好的是一行或一列RadioButton s。你可以在你的佈局中有兩個RadioGroup,並在你的java代碼中監聽兩者的變化。

private RadioGroup mGroup1; // init in onCreate 
private RadioGroup mGroup2; // init in onCreate 

private OnCheckedChangedListener mCheckListener = new OnCheckedChangedListener() { 

    @Override 
    public void onCheckedChanged(RadioGroup group, int checkedId) { 
     // To make it appear as if the two groups are one large group, 
     // checking something in either should clear the check in the other. 
     RadioGroup otherGroup = group == mGroup1 ? mGroup2 : mGroup1; 
     otherGroup.clearCheck(); 

     // do something with checkedId 
    } 
} 
+0

這個group1來自哪裏: RadioGroup otherGroup = group == group1? mGroup2:mGroup1;'? –

+0

@AndyStrife這是一個錯字。固定。 – Karakuri