RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup); for (int i = 0; i 10; i++) { RadioButton radioButton = new RadioButton(this); RadioGroup.LayoutParams lp = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT,
for (int i = 0; i < 10; i++) {
RadioButton radioButton = new RadioButton(this);
RadioGroup.LayoutParams lp = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
//设置RadioButton边距 (int left, int top, int right, int bottom)
lp.setMargins(SizeUtils.dp2px(10), SizeUtils.dp2px(10), SizeUtils.dp2px(10), SizeUtils.dp2px(10));
//设置RadioButton背景
radioButton.setBackgroundResource(R.drawable.radiobutton_background);
radioButton.setTextColor(getResources().getColorStateList(R.drawable.radiobutton_textcolor));
radioButton.setButtonDrawable(null);
//设置文字距离四周的距离
radioButton.setPadding(SizeUtils.dp2px(25), SizeUtils.dp2px(15), SizeUtils.dp2px(25), SizeUtils.dp2px(15));
radioButton.setTextSize(SizeUtils.sp2px(28));
radioButton.setId(i);
radioButton.setChecked(i == 0);
//设置文字
radioButton.setText(i + " #罐");
final int finalI = i;
//设置radioButton的点击事件
radioButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(TankWarningSettingActy.this, "this is radioButton " + finalI + " = " + v.getId(), Toast.LENGTH_SHORT).show();
}
});
//将radioButton添加到radioGroup中
radioGroup.addView(radioButton, lp);
}<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:scrollbars="none">
<RadioGroup
android:id="@+id/radioGroup"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- <RadioButton-->
<!-- android:id="@+id/tank_warning_setting_1"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:layout_margin="@dimen/dp_10"-->
<!-- android:background="@drawable/radiobutton_background"-->
<!-- android:button="@null"-->
<!-- android:checked="true"-->
<!-- android:padding="@dimen/dp_15"-->
<!-- android:text="选项1"-->
<!-- android:textColor="@drawable/radiobutton_textcolor"-->
<!-- android:textSize="@dimen/sp_28" />-->
<!-- <RadioButton-->
<!-- android:id="@+id/tank_warning_setting_2"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:layout_margin="@dimen/dp_10"-->
<!-- android:background="@drawable/radiobutton_background"-->
<!-- android:button="@null"-->
<!-- android:padding="@dimen/dp_15"-->
<!-- android:text="选项1"-->
<!-- android:textColor="@drawable/radiobutton_textcolor"-->
<!-- android:textSize="@dimen/sp_28" />-->
</RadioGroup>
</HorizontalScrollView>
radiobutton_background
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/radiobutton_background_unchecked" android:state_checked="false" />
<item android:drawable="@drawable/radiobutton_background_checked" android:state_checked="true" />
</selector>
radiobutton_background_unchecked
android:shape="rectangle">
<!-- 背景填充 -->
<solid android:color="@color/white" />
<!-- 圆角 -->
<corners android:radius="5dp" />
<stroke
android:width="3dp"
android:color="@color/color_text_gray" /><!-- 描边,边框宽度、颜色 -->
</shape>
radiobutton_background_checked
android:shape="rectangle">
<!-- 填充 -->
<solid android:color="#f59a23" />
<!-- 圆角 -->
<corners android:radius="5dp" />
<!-- <stroke-->
<!-- android:width="3dp"-->
<!-- android:color="@color/color_text_gray" /><!– 描边,边框宽度、颜色 –>-->
</shape>
radiobutton_textcolor
<item android:color="@color/white" android:state_checked="true" />
<item android:color="@color/black" android:state_checked="false" />
</selector>