通常我们给RadioButton 设置一个图片的时候会用到 android:drawableLeft="" 属性,但是这个属性设置的图片如果过大,会导致图片显示一部分。
解决办法如下:
创建style文件
<style name="zzhCustomRadioTheme" parent="@android:style/Widget.CompoundButton.CheckBox"> <item name="android:button">@drawable/radio_mine</item> </style>
radio_mine.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/radio_check" android:state_checked="true"/> <item android:drawable="@drawable/radio_nor" android:state_checked="false" ></item> <item android:drawable="@drawable/radio_nor" ></item> </selector>
在RadioButton 添加
style="@style/zzhCustomRadioTheme"
这样还是不行 还得再java代码中给他限制一下
private void SetView(int LayoutId, RadioButton radioButton) { //定义底部标签图片大小和位置 Drawable drawable_news = getResources().getDrawable(LayoutId); //当这个图片被绘制时,给他绑定一个矩形 ltrb规定这个矩形 drawable_news.setBounds(0, 0, 50, 50); //设置图片在文字的哪个方向 radioButton.setCompoundDrawables(drawable_news,null,null, null); }
LayoutId 就是radio_mine.xml 传 R.drawable.radio_mine就行,radioButton 就是你定义的需要限制图片的RadioButton了
实测没得任何问题,哦还得在xml中的RadioButton里添加
android:button="@null"
隐藏系统默认的图片。