android CheckBox RadioButton 图片与文字间距问题

在使用自定义的CheckBox 或RadioButton时,自定义的图标和文字在不同的手机上显示的间距不同,有时不太好控制,下面是我自定义的CheckBox:

在Layout下的xml:

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <CheckBox   
  2.             android:id="@+id/recharge_activity_cb"  
  3.             style="@style/CustomCheckboxTheme"  
  4.             android:layout_width="wrap_content"  
  5.             android:layout_height="wrap_content"  
  6.             android:text="我已经阅读并同意"  
  7.             android:textColor="@color/huoqiuLightblackColor"  
  8.             android:checked="true"   
  9.             />  

里面自定义的style,style内容为:

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <!-- 自定义CheckBox -->  
  2.     <style name="CustomCheckboxTheme" parent="@android:style/Widget.CompoundButton.CheckBox">  
  3.         <item name="android:button">@drawable/checkbox_style</item>  
  4.         <item name="android:paddingLeft">@dimen/dp8</item>  
  5.     </style>  

其中调用了选中和未选中图片,在drawable下:

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.       
  4.     <item android:state_checked="true" android:drawable="@drawable/agree"/>  
  5.     <item android:state_checked="false" android:drawable="@drawable/disagree"/>  
  6.     <item android:drawable="@drawable/disagree"/>  
  7.   
  8. </selector>  


虽然通过设置paddingLeft在有的系统上可以显示想要的,但是有些则不行
[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <item name="android:paddingLeft">@dimen/dp8</item>  

那么问题来了,到底用什么方法可以解决呐?

下面是我的解决方案:使用CheckedTextView控件

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <CheckedTextView   
  2.             android:id="@+id/recharge_activity_ctv"  
  3.             android:checkMark="@drawable/checkbox_style"  
  4.             android:layout_width="wrap_content"  
  5.             android:layout_height="wrap_content"  
  6.             android:checked="true"  
  7.             />  

[html]  view plain  copy
 print ?
  1. <TextView   
  2.             android:id="@+id/buy_write_jine_agree_tv"  
  3.             android:layout_width="wrap_content"  
  4.             android:layout_height="wrap_content"  
  5.             android:text="我已经阅读并同意"  
  6.             android:textColor="@color/huoqiuLightblackColor"  
  7.             android:layout_marginLeft="@dimen/dp3"  
  8.             />  


其中checkMark就是上面drawable下的那个选中状态xml

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. android:checkMark="@drawable/checkbox_style"  

然后在代码中对CheckedTextView使用isChecked()判断是否选中,使用toggle()方法设置选中和未选中效果,这样就不存在图标和文本的空隙问题了,问题完美解决!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值