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()方法设置选中和未选中效果。这样就不存在图标和文本的空隙问题了,问题完美解决。

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

时间: 2024-08-03 10:56:45

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

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

在使用自定义的CheckBox 或RadioButton时,自定义的图标和文字在不同的手机上显示的间距不同,有时不太好控制,下面是我自定义的CheckBox: 在Layout下的xml: <CheckBox android:id="@+id/recharge_activity_cb" style="@style/CustomCheckboxTheme" android:layout_width="wrap_content" android:

Android checkbox和radiobutton 以及Toast和AlertDialog的使用

package com.example.radiobutton_01; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; publi

android checkBox背景样式及用颜色值实现按钮点击效果

1  使用颜色值(不使用图片)来实现按钮的点击效果: color.xml <color name="head_color">#836FFF</color> <color name="information_title_color">#666666</color> <color name="list_bg_color">#FFF4F4F4</color> <color n

android.widget.RadioButton 单选按钮(转)

大家好,我们今天这一节要介绍的是RadioGroup 的组事件.RadioGroup 可将各自不同的RadioButton ,设限于同一个Radio 按钮组,同一个RadioGroup 组里的按钮,只能做出单一选择(单选题). 首先,我们先设计一个TextView Widget ,以及一个RadioGroup ,并将该RadioGroup 内放置两个RadioButton ,默认为都不选择,在程序运行阶段,利用onCheckedChanged 作为启动事件装置,让User选择其中一个按钮,显示被

转:android 自定义RadioButton样式

http://gundumw100.iteye.com/blog/1146527  上面这种3选1的效果如何做呢?用代码写? 其实有更简单的办法,忘了RadioButton有什么特性了吗? 我就用RadioButton实现了如上效果,其实很简单的. 首先定义一张background,命名为radio.xml,注意该background必须为xml样式的图片: Xml代码   <?xml version="1.0" encoding="utf-8"?> &

Android实现限制EditText输入文字的数量

一: 声明控件. TextView hasnumTV; TextView hasnum;// 用来显示剩余字数 int num = 50;// 限制的最大字数 二: 主要的方法: hasnumTV = (TextView) findViewById(R.id.tv_num); hasnumTV.setText("限" + num + "" + "字以内"); editText.addTextChangedListener(new TextWatc

Android开发之自定义TabHost文字及背景(源代码分享)

使用TabHost 可以在一个屏幕间进行不同版面的切换,而系统自带的tabhost界面较为朴素,我们应该如何进行自定义修改优化呢 MainActivity的源代码 package com.dream.ledong; import android.app.TabActivity; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.view.Gr

android checkbox 小记

? 1 2 3 4 5 6 7 <CheckBox      android:id="@+id/cbCheckBox"      android:layout_width="0px"      android:layout_height="wrap_content"      android:layout_weight="1"      android:button="@drawable/checkbox_ch

android如何写一个循环文字滚动的TextView

效果图: 在layout中这样来声明: <com.kaixin001.view.ScrollText android:id="@+id/news_statustxt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dp" android:paddingRight="1