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-28 13:21:17

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" andr

Android TextView中有图片有文字混合排列

1.使用html.fromHtml 2.新建ImageGetter 3.使用<img src>标签 demo: 1.设置文字 ((TextView) findViewById(R.id.tv_gradlist_calorie_desc)).setText(Html .fromHtml(descString(), getImageGetterInstance(), null)); 2.获取文字 /** * 字符串 * * @return */ private String descString(

解决Android中RadioButton图片和文本居中及底部经常被工具烂覆盖掉内容

1.图片和文本居中,只需要如下设置即可 <RadioButton android:id="@+id/myid" style="@style/tab_bottom" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:background=&qu

Android实现分享图片和文字的功能

为了应用的推广,我们经常看到点击分享按钮会出现,比如微博微信等应用的分享二等列表,这是如何实现的呢?这一篇将要详细的介绍. android的实现分享是通过隐式的启动activity. 分享文本 1.action是action_send,相应的代码: Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_TEXT, "hello wor

android控件布局 ToggleButton按钮 图片和文字

ToggleButton控件具有checked属性对于要切换状态的功能按钮来说十分好用 这里以phone的开启DTMFTwelveKeyDialer的按钮为例 展示其效果和布局 activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" andro

Android APP 分享图片文字到微信刚开始正常,后面就不弹出分享框了

按照官方的流程走了一遍,一切顺利,分享成功了,本来以为可以大功告成了,结果睡了一觉,第二天要给客户演示了,才发现TMD坑爹了,不能分享了,第三方的分享栏弹不出来了,我一阵惊慌,还好很快找到了解决办法:原因是我进行了代码混淆,但是没有对新添加的这部分分享到微信的代码进行处理,所以...解决问题的办法很简单:需要在混淆配置文件proguard.cfg中,增加如下两行代码: -keep class com.tencent.mm.sdk.openapi.WXMediaMessage { *;} -kee

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 TextView 设置文字间距

搜索结果都不靠谱,基本上都说是android:textScaleX可以设置文字间距,也不知道你们到底有没有真正做过开发. Android开发文档里描述的很清楚: android:textScaleX Sets the horizontal scaling factor for the text. 这个东西是用来设置水平方向的文字放大和缩小的. 其实设置文字间距可以通过修改string.xml里的字符,在其中添加空格来实现. 但是在字符串中直接输入空格达不到想要的效果,这就需要使用转义字符来进行转

android 使用Intent.ACTION_SEND分享图片和文字内容(新浪微博,短信等)

下面的方法只能实现普通的文字分享: 1 2 3 4 5 6 7 8 9 10 11 private void shareContent() {         Intent share = new Intent(android.content.Intent.ACTION_SEND);         share.setType("text/plain");         String title = "标题";         String extraText=&