效果展示
布局文件的书写==”fill_parent”android:layout_height=”fill_parent”><RadioGroup===”10dp”android:paddingBottom=”10dp”><RadioButton====”center”android:paddingLeft=”5dp”/><RadioButton====”@string/down_name”android:textColor=”#272727″/><RadioButton====”@string/ting_name”android:textColor=”#272727″/><RadioButton=====”#272727″android:checked=”false”/></RadioGroup></RelativeLayout>radiobutton背景选择器==>></selector>==>></selector>==>></selector>==>></selector>activity书写package 150427;import androidimport androidimport androidimport androidimport androidimport androidpublic class MainActivity extends Activity {/*** Called when the activity is first created.*/@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radiogroup);RadioButton radioButton01 = (RadioButton) findViewById(R.id.rabt01);RadioButton radioButton02 = (RadioButton) findViewById(R.id.rabt02);RadioButton radioButton03 = (RadioButton) findViewById(R.id.rabt03);RadioButton radioButton04 = (RadioButton) findViewById(R.id.rabt04);radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup radioGroup, int checkID) {switch (checkID) {case R.id.rabt01:radioButton01.setTextColor(Color.rgb(220, 20, 60));radioButton02.setTextColor(Color.rgb(0, 0, 0));radioButton03.setTextColor(Color.rgb(0, 0, 0));radioButton04case R.id.rabt02:radioButton02.setTextColor(Color.rgb(220, 20, 60));radioButton01.setTextColor(Color.rgb(0, 0, 0));radioButton03.setTextColor(Color.rgb(0, 0, 0));radioButton04case R.id.rabt03:radioButton03.setTextColor(Color.rgb(220, 20, 60));radioButton01.setTextColor(Color.rgb(0, 0, 0));radioButton02.setTextColor(Color.rgb(0, 0, 0));radioButton04case R.id.rabt04:radioButton04.setTextColor(Color.rgb(220, 20, 60));radioButton01.setTextColor(Color.rgb(0, 0, 0));radioButton02.setTextColor(Color.rgb(0, 0, 0));radioButton03}}});}}改进
我们也可以把字体颜色的改变写一个背景选择器,这样就可以省去在代码中写监听器了。
项目代码下载地址
https://github.com/zhujainxipan/radiogroup
知识讲解
在android开发中,往往我们需要自定义RadioButton中button图片和文字的距离,经过多番查找资料,得知解决办法如下:
另外就是android选择器的知识点
radiogroup中如果嵌套布局,radiobutton就可以多选了,就不是我们想要的效果了。
android对资源文件的大小有限制,图片最大1m。
感恩:
RadioButton设置了gravity=center,,发现文字还不居中,求指导
Android RadioGroup中的RadioButton竟然能多选?
Android中如何设置RadioButton在文字的右边,图标在左边?
Android自定义radiobutton(文字靠左,选框靠右)
自定义RadioButton样式并去除默认样式位置
android自定义RadioGroup的RadioButton中文字和图片的距离
Android radiobutton图片与文字间距的问题
不是每一次努力都有收获,但是,每一次收获都必须经过努力。