google官方建议在textView和imageView挨着的时候,建议使用drawable来显示图片

第一个方法:setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom)

  api原文为:

Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use null if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.

意思大概就是:可以在上、下、左、右设置图标,如果不想在某个地方显示,则设置为null。图标的宽高将会设置为固有宽高,既自动通过getIntrinsicWidth和getIntrinsicHeight获取。——笔者翻译

   button = (RadioButton) group.getChildAt(i);
   Resources res = TabTest.this.getResources();
   Drawable myImage = res.getDrawable(R.drawable.home);
   button.setCompoundDrawablesWithIntrinsicBounds(null, myImage, null, null);

第二种方法:setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)

  api原文为:

Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use null if you do not want a Drawable there. The Drawables must already have had setBounds(Rect) called.

意思大概就是:可以在上、下、左、右设置图标,如果不想在某个地方显示,则设置为null。但是Drawable必须已经setBounds(Rect)。意思是你要添加的资源必须已经设置过初始位置、宽和高等信息。——笔者翻译

  这下就明白了,这个方法要先给Drawable设置setBounds(x,y,width,height);

  x:组件在容器X轴上的起点 y:组件在容器Y轴上的起点 width:组件的长度 height:组件的高度。

  如代码:

1  Resources res = TabTest.this.getResources();
2 Drawable myImage = res.getDrawable(R.drawable.home);
3 myImage.setBounds(1, 1, 100, 100);
4 button.setCompoundDrawables(null, myImage, null, null);

设置drawable图片的更多相关文章

  1. Android开发 从代码里设置Drawable图片不显示的问题

    问题描述 我们从代码里获得Drawable在设置给View时会发现,图片不显示的问题.比如如下代码: Drawable drawable = getResources().getDrawable(R. ...

  2. textview设置drawable

    textview可以在上下左右四个方向添加图片,同时也可以动态改变这些图片:   下面有我写的一个例子: 在xml文件中:  <TextView                 android: ...

  3. [Netbeans]为面板设置背景图片

    与AndroidStudio等类似IDE不同,在Netbeans开发桌面程序时,不可以直接通过src=@drawable 等方法为窗口设置背景图片.这里介绍一种简便的方法: 1:首先,拖动一个面板到f ...

  4. Android代码中设置背景图片

    //设置背景图片        String picfile= Environment.getExternalStorageDirectory() + "/pdp/pdp.png" ...

  5. 使用装饰器模式动态设置Drawable的ColorFilter

    使用装饰器模式动态设置Drawable的ColorFilter 欢迎各位关注我的新浪微博:微博 转载请标明出处(kifile的博客) 非常多时候我们都希望Android控件点击的时候,有按下效果,选中 ...

  6. img只显示图片一部分 或 css设置背景图片只显示图片指定区域

    17:14 2016/3/22img只显示图片一部分 或 css设置背景图片只显示图片指定区域 background-position: 100% 56%; 设置背景图片显示图片的哪个坐标区域,图片左 ...

  7. 为窗体设置背景图片-UI界面编辑器(SkinStudio)教程

    1.1.   为窗体设置背景图片 在窗体的Background属性中选择图片设置为窗体背景图片

  8. 新浪微博客户端(32)-设置相册图片的contentMode

    DJStatusPhotoView.m #import "DJStatusPhotoView.h" #import "UIImageView+WebCache.h&quo ...

  9. UIView 设置背景图片

    http://blog.csdn.net/qijianli/article/details/7777268 项目中,可能需要我们为某个视图设置背景图片,而API中UIView没有设置背景图片的方法,那 ...

随机推荐

  1. heartbeat安装

    wget ftp://mirror.switch.ch/pool/1/mirror/scientificlinux/6rolling/i386/os/Packages/epel-release-6-5 ...

  2. 绑定本地Service并与之通信-----之一

    import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os. ...

  3. POJ 1860 Currency Exchange 最短路 难度:0

    http://poj.org/problem?id=1860 #include <cstdio> //#include <queue> //#include <deque ...

  4. Right-BICEP 测试四则运算二程序

    测试方法: Right-BICEP 测试计划: 1.Right-结果是否正确? 2.B-是否所有的边界条件都是正确的? 3.是否有乘除法? 4.是否有括号? 5.是否有输出方式? 6.是否可以选择出题 ...

  5. Word2013可以写博客

    步骤如下http://www.cnblogs.com/guyichang/p/4629211.html

  6. Matlab与C/C++联合编程之Matlab以MEX方式调用C/C++代码(三)

    最近写了个Matlab程序,好慢呐……所以开始学习Matlab与C/C++混合编程.下面写了个测试代码,显示一个Double类型矩阵中的元素. 源代码 #include "mex.h&quo ...

  7. [C/C++]C++标准中的名词

    1.qualified-id.nested-name-specifier: [example: struct A { struct B { void F(); }; }; A is an unqual ...

  8. Tomcat 网站部署(三)

    一.Tomcat的部署方式有以下两种 1.自动部署 2.虚拟目录 二.自动部署 文件必须放在放在webapps就可以了,可以用这样访问 http://localhost:8080/放在webapps目 ...

  9. IOS(SystemConfiguration)框架中关于测试连接网络状态相关方法

    1. 在SystemConfiguration.famework中提供和联网相关的function, 可用来检查网络连接状态. 2. SC(SystemConfiguration)框架中关于测试连接网 ...

  10. PHP Forms

    <html><body><form action="welcome.php" method="post">Name: < ...