android CheckBox RadioButton 照片和文字的间距问题

利用自身的定义CheckBox 要么RadioButton时间。定义自己的图标和文字在不同的手机显示不同的音高。有时不太好控制,下面是我自己的定义CheckBox:

在Layout在下面xml:

<CheckBox 
            android:id="@+id/recharge_activity_cb"
            style="@style/CustomCheckboxTheme"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="我已经阅读并允许"
            android:textColor="@color/huoqiuLightblackColor"
            android:checked="true" 
            />

里面自己定义的style。style内容为:

<!-- 自己定义CheckBox -->
    <style name="CustomCheckboxTheme" parent="@android:style/Widget.CompoundButton.CheckBox">
        <item name="android:button">@drawable/checkbox_style</item>
        <item name="android:paddingLeft">@dimen/dp8</item>
    </style>

当中调用了选中和未选中图片。在drawable下:

<?

xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_checked="true" android:drawable="@drawable/agree"/> <item android:state_checked="false" android:drawable="@drawable/disagree"/> <item android:drawable="@drawable/disagree"/> </selector>


尽管通过设置paddingLeft在有的系统上能够显示想要的,可是有些则不行
<item name="android:paddingLeft">@dimen/dp8</item>

那么问题来了,究竟用什么方法能够解决呐?

以下是我的解决方式:使用CheckedTextView控件

<CheckedTextView 
            android:id="@+id/recharge_activity_ctv"
            android:checkMark="@drawable/checkbox_style"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            />

<TextView 
            android:id="@+id/buy_write_jine_agree_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="我已经阅读并允许"
            android:textColor="@color/huoqiuLightblackColor"
            android:layout_marginLeft="@dimen/dp3"
            />


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

android:checkMark="@drawable/checkbox_style"

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



版权声明:本文博客原创文章,博客,未经同意,不得转载。

原文地址:https://www.cnblogs.com/bhlsheji/p/4670838.html