设置drawable图片
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 意思大概就是:可以在上、下、左、右设置图标,如果不想在某个地方显示,则设置为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图片的更多相关文章
- Android开发 从代码里设置Drawable图片不显示的问题
问题描述 我们从代码里获得Drawable在设置给View时会发现,图片不显示的问题.比如如下代码: Drawable drawable = getResources().getDrawable(R. ...
- textview设置drawable
textview可以在上下左右四个方向添加图片,同时也可以动态改变这些图片: 下面有我写的一个例子: 在xml文件中: <TextView android: ...
- [Netbeans]为面板设置背景图片
与AndroidStudio等类似IDE不同,在Netbeans开发桌面程序时,不可以直接通过src=@drawable 等方法为窗口设置背景图片.这里介绍一种简便的方法: 1:首先,拖动一个面板到f ...
- Android代码中设置背景图片
//设置背景图片 String picfile= Environment.getExternalStorageDirectory() + "/pdp/pdp.png" ...
- 使用装饰器模式动态设置Drawable的ColorFilter
使用装饰器模式动态设置Drawable的ColorFilter 欢迎各位关注我的新浪微博:微博 转载请标明出处(kifile的博客) 非常多时候我们都希望Android控件点击的时候,有按下效果,选中 ...
- img只显示图片一部分 或 css设置背景图片只显示图片指定区域
17:14 2016/3/22img只显示图片一部分 或 css设置背景图片只显示图片指定区域 background-position: 100% 56%; 设置背景图片显示图片的哪个坐标区域,图片左 ...
- 为窗体设置背景图片-UI界面编辑器(SkinStudio)教程
1.1. 为窗体设置背景图片 在窗体的Background属性中选择图片设置为窗体背景图片
- 新浪微博客户端(32)-设置相册图片的contentMode
DJStatusPhotoView.m #import "DJStatusPhotoView.h" #import "UIImageView+WebCache.h&quo ...
- UIView 设置背景图片
http://blog.csdn.net/qijianli/article/details/7777268 项目中,可能需要我们为某个视图设置背景图片,而API中UIView没有设置背景图片的方法,那 ...
随机推荐
- [js]多个物体的运动
与单个的区别:得知道哪个在动,所以运动函数需要两个参数,出了目标iTarget之外,还要obj.另外需要多个计数器,否则当一个还没运动完就移入另一个物体会发生卡壳 window.onload=func ...
- 转: Div与table的区别
1:速度和加载方式方面的区别 div 和 table 的差异不是速度,而是加载方式,速度只能是指网络速度,如果速度足够快,是没有差异的: div 的加载方式是即读即加载,遇到 <div> ...
- matlab:clear,close,clc
clear 删除工作空间中的项目,释放系统内存 语法: clear clear name clear name1 name2 name3... clear global name clear -reg ...
- ssh原理
客户端向服务器端发出连接请求 服务器端向客户端发出自己的公钥 客户端使用服务器端的公钥加密通讯密钥然后发给服务器端 如果通讯过程被截获,由于窃听者即使获知公钥和经过公钥加密的内容,但不拥有私 ...
- namenode 无法启动之每次开机需要重新格式化-tmp
最近遇到了一个问题,执行start-all.sh的时候发现JPS一下namenode没有启动 每次开机都得重新格式化一下namenode才可以 其实问题就出在tmp文件,默 ...
- NSURLSession的使用
虽然在iOS7引入NSURLSession时,就知道NSURLConnection会最终被苹果放弃,但人总是喜欢做熟悉的事情,在NSURLConnection还可以使用时,就懒得学习这新玩意了,而且本 ...
- java基础-007
41.Servlet Servlet 是处理客户端请求并产生动态网页内容的Java类.Servlet主要是用来处理或者存储HTML表单提交的数据,产生动态内容,在无状态的HTTP协议下管理状态信息.所 ...
- php的字符串处理
字符串处理: strlen("aaa");取字符串的长度 *** strcmp("aaa","aaa");比较两个字符串,相同的话输出0,不 ...
- Git 的安装和创建版本库 。
Git 的优点就不再多说了 .直接进入正题吧 . 安装Git 首先可以尝试输入 Git 看看有没有反映 . $ git The program 'git' is currently not insta ...
- yii2 数据验证
控制器层 <?php namespace frontend\controllers; use Yii; use frontend\models\FormsModel; use yii\web\U ...