记录下实现过程,因为最近项目里要用到。我们都知道默认的RadioGroup+RadioButton是不能实现轻松换行的。如果每行使用一个RadioGroup来包裹RadioButton的话。其中的选择监听是个非常麻烦的事情。那么今天记录下RadioButton+GridLayout。
首先xml布局中添加一个GridLayout控件
<GridLayout
android:id="@+id/radioGridLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="2"
android:padding="16dp"
android:alignmentMode="alignMargins"
android:columnOrderPreserved="false"
android:rowOrderPreserved="false">
</GridLayout>
接着在activity中,添加下面的代码就可以了。要几个就加几个,不一定单行得满,比如3+2个也可以,3+3个也可以。
private List<RadioButton> radioButtons = new ArrayList<>();//RadioButton集合
protected void onCreate(@Nullable Bundle savedInsta