关注微信号:javalearns?? 随时随地学Java
或扫一扫
?
?
随时随地学Java
在使用自定义的CheckBox 或RadioButton时,自定义的图标和文字在不同的手机上显示的间距不同,有时不太好控制,下面是我自定义的CheckBox:
在Layout下的xml:
1 2 3 4 5 6 7 8 9 10 | <CheckBox ???????? <a title= "android" ?href= "http://www.android-study.com/jiemiansheji/599.html" >android</a>:id= "@+id/recharge_activity_cb" ???????? style= "@style/CustomCheckboxTheme" ???????? <a title= "android" ?href= "http://www.android-study.com/jiemiansheji/599.html" >android</a>:layout_width= "wrap_content" ???????? <a title= "android" ?href= "http://www.android-study.com/jiemiansheji/599.html" >android</a>:layout_height= "wrap_content" ???????? <a title= "android" ?href= "http://www.android-study.com/jiemiansheji/599.html" >android</a>:text= "我已经阅读并同意" ???????? <a title= "android" ?href= "http://www.android-study.com/jiemiansheji/599.html" >android</a>:textColor= "@color/huoqiuLightblackColor" ???????? <a title= "android" ?href= "http://www.android-study.com/jiemiansheji/599.html" >android</a>:checked= "true" ???????? /> ??????? ? |
里面自定义的style,style内容为:
1 2 3 4 5 6 | <!-- 自定义CheckBox --> <style name= "CustomCheckboxTheme" ?parent= "@<a title=" android " href=" http: //www.android-study.com/jiemiansheji/599.html">android</a>:style/Widget.CompoundButton.CheckBox"> ???? <item name= "<a title=" android " href=" http: //www.android-study.com/jiemiansheji/599.html">android</a>:button">@drawable/checkbox_style</item> ???? <item name= "<a title=" android " href=" http: //www.android-study.com/jiemiansheji/599.html">android</a>:paddingLeft">@dimen/dp8</item> </style> |
其中调用了选中和未选中图片,在drawable下:
1 2 3 4 5 6 7 8 9 | ???? <?xml version= "1.0" ?encoding= "utf-8" ?> <selector xmlns:<a title= "android" ?href= "http://www.android-study.com/jiemiansheji/599.html" >android</a>= "http://schemas.<a title=" android " href=" http://www.android-study.com/jiemiansheji/ 599 .html ">android</a>.com/apk/res/<a title=" android " href=" http://www.android-study.com/jiemiansheji/ 599 .html ">android</a>" ?> ???? ????? <item <a title= "android" ?href= "http://www.android-study.com/jiemiansheji/599.html" >android</a>:state_checked= "true" ?<a title= "android" ?href= "http://www.android-study.com/jiemiansheji/599.html" >android</a>:drawable= "@drawable/agree" /> ???? <item <a title= "android" ?href= "http://www.android-study.com/jiemiansheji/599.html" >android</a>:state_checked= "false" ?<a title= "android" ?href= "http://www.android-study.com/jiemiansheji/599.html" >android</a>:drawable= "@drawable/disagree" /> ???? <item <a title= "android" ?href= "http://www.android-study.com/jiemiansheji/599.html" >android</a>:drawable= "@drawable/disagree" /> ? </selector> ??? ? |
虽然通过设置paddingLeft在有的系统上可以显示想要的,但是有些则不行
1 2 | <item name= "<a title=" android " href=" http: //www.android-study.com/jiemiansheji/599.html">android</a>:paddingLeft">@dimen/dp8</item> |
那么问题来了,到底用什么方法可以解决呐?
下面是我的解决方案:使用CheckedTextView控件
1 2 3 4 5 6 7 8 | <CheckedTextView ???????? <a title= "android" ?href= "http://www.android-study.com/jiemiansheji/599.html" >android</a>:id= "@+id/recharge_activity_ctv" ???????? <a title= "android" ?href= "http://www.android-study.com/jiemiansheji/599.html" >android</a>:checkMark= "@drawable/checkbox_style" ???????? <a title= "android" ?href= "http://www.android-study.com/jiemiansheji/599.html" >android</a>:layout_width= "wrap_content" ???????? <a title= "android" ?href= "http://www.android-study.com/jiemiansheji/599.html" >android</a>:layout_height= "wrap_content" ???????? <a title= "android" ?href= "http://www.android-study.com/jiemiansheji/599.html" >android</a>:checked= "true" ???????? /> ??????????? ? |
1 2 3 4 5 6 7 8 9 | <TextView <a title= "android" ?href= "http://www.android-study.com/jiemiansheji/599.html" >android</a>:id= "@+id/buy_write_jine_agree_tv" <a title= "android" ?href= "http://www.android-study.com/jiemiansheji/599.html" >android</a>:layout_width= "wrap_content" <a title= "android" ?href= "http://www.android-study.com/jiemiansheji/599.html" >android</a>:layout_height= "wrap_content" <a title= "android" ?href= "http://www.android-study.com/jiemiansheji/599.html" >android</a>:text= "我已经阅读并同意" <a title= "android" ?href= "http://www.android-study.com/jiemiansheji/599.html" >android</a>:textColor= "@color/huoqiuLightblackColor" <a title= "android" ?href= "http://www.android-study.com/jiemiansheji/599.html" >android</a>:layout_marginLeft= "@dimen/dp3" /> ??? ? |
其中checkMark就是上面drawable下的那个选中状态xml
1 2 | <a title= "android" ?href= "http://www.android-study.com/jiemiansheji/599.html" >android</a>:checkMark= "@drawable/checkbox_style" |
然后在代码中对CheckedTextView使用isChecked()判断是否选中,使用toggle()方法设置选中和未选中效果,这样就不存在图标和文本的空隙问题了,问题完美解决!
关注微信号:javalearns?? 随时随地学Java
或扫一扫
?
?
随时随地学Java