RadioGroup默认的横向android:orientation="horizontal"、竖向布局android:orientation="vertical",无法设置每行显示的个数,只能全部横向显示或者竖向显示。
如何才能显示两个并有单选的效果呢? 那就需要用好 android:layout_marginLeft 和android:layout_marginTop这两条属性了。
看一下效果图:
实现代码:
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton
android:id="@+id/excellent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"
android:text="优:" />
<RadioButton
android:id="@+id/fine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:layout_marginTop="-48dp"
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"
android:text="良:" />
<RadioButton
android:id="@+id/general"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"
android:text="一般:" />
<RadioButton
android:id="@+id/bad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:layout_marginTop="-48dp"
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"
android:text="差:" />
</RadioGroup>