自己定义checkbox中的勾选框图标。这次由于想偷懒。图标弄的大了些。然后一系列的问题就都引出来了。

1、图标比checkbox的layout_height高。看不见了。

非常吐血吧,CompoundButton中的源代码能够看到以下代码

protected void onDraw(Canvas canvas) {
super.onDraw(canvas); final Drawable buttonDrawable = mButtonDrawable;
if (buttonDrawable != null) {
final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
final int height = buttonDrawable.getIntrinsicHeight();//能够看到是依据图片的原始高度对图片进行绘制的,,danielinbiti int y = 0; switch (verticalGravity) {
case Gravity.BOTTOM:
y = getHeight() - height;
break;
case Gravity.CENTER_VERTICAL:
y = (getHeight() - height) / 2;
break;
} buttonDrawable.setBounds(0, y, buttonDrawable.getIntrinsicWidth(), y + height);
buttonDrawable.draw(canvas);
}
}

从这里能够看到是依据图片的高度进行绘制的。而不是依据你设置的layout_height进行图片缩放。

所以解决的途径也就两种:

第一种方法:把原始图片缩小,这就就是制作图片了

另外一种方法:重写onDraw方法

@Override
protected void onDraw(Canvas canvas) {
setButtonDrawable(parentds);//danielinbiti,<span style="font-family: Arial, Helvetica, sans-serif;">parentds是</span><span style="font-family: Arial, Helvetica, sans-serif;">occupyPosDrawable方法返回的。 </span><span style="font-family: Arial, Helvetica, sans-serif;">
</span> super.onDraw(canvas);
setButtonDrawable(ds);//真正要显示的图片
final Drawable buttonDrawable = ds;
if (buttonDrawable != null) {
final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
final int drawableHeight = buttonDrawable.getIntrinsicHeight()>selfHeight
? selfHeight:buttonDrawable.getIntrinsicHeight();
final int drawableWidth = buttonDrawable.getIntrinsicWidth()>selfHeight
? selfHeight:buttonDrawable.getIntrinsicWidth();
//<span style="font-family: Arial, Helvetica, sans-serif;">selfHeight为要定义的图片高度</span>
int top = 0;
switch (verticalGravity) {
case Gravity.BOTTOM:
top = getHeight() - drawableHeight;
break;
case Gravity.CENTER_VERTICAL:
top = (getHeight() - drawableHeight) / 2;
break;
} int left = 0;
int right = left+drawableWidth;
int bottom = top+drawableHeight; buttonDrawable.setBounds(left, top, right, bottom);
buttonDrawable.draw(canvas);
}
}
// drawable 转换成bitmap
private Drawable occupyPosDrawable(int height){//
Picture p=new Picture();
Canvas c=p.beginRecording(height,height);
p.endRecording(); PictureDrawable pd=new PictureDrawable(p); return pd;
}

2、当这个解决后。自以为已经完毕了。结果定义了paddingLeft后。问题出现了。在4.1.2版本号不正常了(图片和文字叠在一起了)。在4.2.2版本号中正常显示了paddingLeft

这真的是花了不少时间,结果发现4.1.2版本号中CompoundButton没有getCompoundPaddingLeft。这不坑人嘛。

两个版本号的计算方式不一样,4.2.2版本号绘制文字时。会把图片的宽度和paddingLeft的宽度加上,而4.1.2版本号仅仅计算设置的paddingLeft,从这里看也是4.2.2版本号人性化一些。

于是又查了一下前后版本号,发现4.1.2版本号是分界线。兴许版本号CompoundButton都有getCompoundPaddingLeft方法。

于是又重写了getCompoundPaddingLeft方法。依据版本号推断

@Override
public int getCompoundPaddingLeft() {
int padding = super.getCompoundPaddingLeft();
if(getAndroidSDKVersion()<=16){//4.1.2
if (!isLayoutRtl()) {
padding = selfHeight + padding;
}
}
return padding;
}

到这里。基本就定义了一个自己的checkbox。

图片大小就不用太在意了,能够依据自己设定的大小把对图片进行缩放。在app应用中相对照较灵活些,同一时候也发现了一个android的版本号差异性问题。曾经用checkbox的时候基本没有设置过paddingLeft,还真没有注意这个问题(由于不设置两个版本号都正常。仅仅是贴这图片)

android中checkbox的padding引发的问题的更多相关文章

  1. android中checkbox自定义样式

    1.首先res/drawable中定义checkbox_style.xml样式: <?xml version="1.0" encoding="utf-8" ...

  2. 抛砖引玉:关于Android的ListView中CheckBox错乱

    首先:参考了这篇翻译的文章:http://www.cnblogs.com/xiaowenji/archive/2010/12/08/1900579.html 文章中关于说的Android中的Recyc ...

  3. android UI进阶之实现listview中checkbox的多选与记录

    今天继续和大家分享涉及到listview的内容.在很多时候,我们会用到listview和checkbox配合来提供给用户一些选择操作.比如在一个 清单页面,我们需要记录用户勾选了哪些条目.这个的实现并 ...

  4. 【转】android UI进阶之实现listview中checkbox的多选与记录--不错

    原文网址:http://www.cnblogs.com/notice520/archive/2012/02/17/2355415.html 今天继续和大家分享涉及到listview的内容.在很多时候, ...

  5. [转]Android中自定义checkbox样式

    android中自定义checkbox的图片和大小   其实很简单,分三步: 1.在drawable中创建文件checkbox_selector.xml: <?xml version=" ...

  6. Android中ListView结合CheckBox判断选中项

    本文主要实现在自定义的ListView布局中加入CheckBox控件,通过判断用户是否选中CheckBox来对ListView的选中项进行相应的操作.通过一个Demo来展示该功能,选中ListView ...

  7. android中的margin和padding

    Android的Margin和Padding跟Html的是一样的.如下图所示:黄色部分为Padding,灰色部分为Margin. 通俗的理解: Padding 为内边框,指该控件内部内容,如文本/图片 ...

  8. Android 中常见控件的介绍和使用

    1 TextView文本框 1.1 TextView类的结构 TextView 是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域.TextView类的层次关系如下: java.la ...

  9. android中的一些问题

    1. Android dvm的进程和Linux的进程, 应用程序的进程是否为同一个概念 DVM指dalivk的虚拟机.每一个Android应用程序都在它自己的进程中运行,都拥有一个独立的Dalvik虚 ...

随机推荐

  1. Codeforces Round #299 (Div. 2) A. Tavas and Nafas 水题

    A. Tavas and Nafas Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/535/pr ...

  2. 使用牛顿迭代法和二分法求解一个数的平方根(python语言实现)

    #牛顿迭代法 def sqrt1(x): y = 1.0 while abs(y * y - x) > 1e-6: y = (y + x/y)/2 return y #使用二分法 def sqr ...

  3. debian禁止或者允许指定ip访问远程mysql、ssh、rsynccat /etc/xinetd.conf

    如果没有安装xinetd,安装xinetd apt-get install xinetd 然后创建配置文件 vi /etc/xinetd.d/mysqld service login { socket ...

  4. Ext如何动态添加一行组件

    用的column布局,点击一个按钮能添加一行组件,如文本框,有下拉框等. 如: 效果: 实现方法如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...

  5. Spring JdbcTemplate+JdbcDaoSupport实例

    在Spring JDBC开发中,可以使用 JdbcTemplate 和 JdbcDaoSupport 类来简化整个数据库的操作过程. 在本教程中,我们将重复上一篇文章Spring+JDBC例子,看之前 ...

  6. RapidSVN的使用

    RapidSVN是一个可视化 SVN 客户端,运行速度快,简单易用,现在很多已经做成做成便携版,安装都不用.小乌龟那个SVN感觉太复杂了...不妨试下这个. 1.下载安装 2.运行RapidSVNPo ...

  7. 简单Gif制作

    没什么需求,只是循环图片的推荐:http://gif.55.la/ ,在线制作,无需下载

  8. ARC中的@autoreleasepool还有作用吗?

    ARC中的@autoreleasepool还有作用吗? QUESTION For the most part with ARC (Automatic Reference Counting), we d ...

  9. 深度学习教程Deep Learning Tutorials

    Deep Learning Tutorials Deep Learning is a new area of Machine Learning research, which has been int ...

  10. OpenCV使用二维特征点(Features2D)和单映射(Homography)寻找已知物体

    使用二维特征点(Features2D)和单映射(Homography)寻找已知物体 目标 在本教程中我们将涉及以下内容: 使用函数 findHomography 寻找匹配上的关键点的变换. 使用函数  ...