代码中修改TextView的DrawableLeft图片
16lz
2021-01-23
先把解决代码贴上来:
Drawable weather = getResources().getDrawable(R.drawable.sunday); weather.setBounds(0, 0, weather.getMinimumWidth(), weather.getMinimumWidth()); tv_choose_weather.setCompoundDrawables(weather, null, null, null);
/***********分割线*********************/
本来觉得在TextView中添加一个android:drawableLeft="@drawable/org3_ww0"
属性比一个ImageView+一个TextView方便多了,结果今天需要更换TextView的DrawableLeft图片时傻眼了,遍访名医后方得解法,记录如下:
TextView有个方法叫setCompoundDrawables(left,top,right,bottom)
就是用来设置、修改他旁边的图片的,我们只需要把新的Drawable传到对应的参数位置即可。
Drawable可以通过getResources().getDrawable(id)方法得到,例如:
Drawable weather = getResources().getDrawable(R.drawable.sunday);
你以为这就结束了?No
setCompoundDrawables() 的参数Drawable对象,必须先调用setBounds(int left, int top, int right, int bottom)方法,设置好这个图片要绘制的矩形区域大小。
所以就有了解决代码的第二行:
weather.setBounds(0, 0, weather.getMinimumWidth(), weather.getMinimumWidth());
对了,那个setBounds的参数怎么传呢?
其实他让你传入的是四个顶点坐标,然后编译器进行运算求出矩形的长宽。我们可以直接在left、top传入0,right、bottom传入要绘制图片的宽和高就行了。
更多相关文章
- java和Android文件下载断点续传和图片下载代码实现,可直接复制简
- Android调用系统相机onActivityResult返回参数data为null
- android 存储图片到data目录和读取data目录下的图片
- android中简单的图片翻页效果
- android从相册中获取图片并将其显示在当前界面
- 如何让Android支持GIF图片
- Android实现下载图片并保存到SD卡中