在使用radiobutton的按钮跟文字之间的间距在不同的手机上会出现间距不一致,今天学习到了如何解决这个问题:
android:button=@null;//将默认的button图片清空
android:drawableLeft=@drawable/radiobutton;//使用该属性定义button图片
android:background=@null;//将radioButton的背景设为空
android:drawablePadding=6dp;//将文字和左侧的button图片相距6dp
button/drawableLeft/background/drawablePadding结合使用方可改变文字和图片的距离 ;
一、xml文件
定义radio的样式、属性:
center_vertical
@null
0dp
@null
@drawable/package_radio_selector
10dp
package_radio_selector.xml的样式:
radioButton的使用:
style="@style/package_radio_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试"
android:textColor="@color/white"
android:textSize="15sp" />
样式的效果:
二、java方法
使用java创建的radioButton 解决图片与文字间距问题:
RadioButton radioButton2 = new RadioButton(this);
radioButton2.setText("测试");
radioButton2.setTextColor(getResources().getColor(R.color.white));
radioButton2.setTextSize(PLEX_UNIT_SP, 15);
radioButton2.setPadding(0, 0, 0, 0);
//radioButton2.setButtonDrawable(null); java设置ButtonDrawable为null,还是会显示原生的radio图标。设置为透明就好了。
radioButton2.setButtonDrawable(getResources().getDrawable(android.R.color.transparent));
radioButton2.setBackgroundDrawable(null);
radioButton2.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.package_radio_selector), null, null, null);
radioButton2.setCompoundDrawablePadding(10);