Android studio 用控件TextView 添加图片
时间: 2024-03-14 15:50:03 浏览: 28
你可以使用TextView的`setCompoundDrawables()`方法来在TextView中添加图片。这个方法需要四个参数,分别是左侧、顶部、右侧和底部的Drawable对象。
以下是一个示例代码,演示如何在TextView中添加左侧的图片:
```java
TextView textView = findViewById(R.id.textview);
Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
textView.setCompoundDrawables(drawable, null, null, null);
```
在这个示例中,我们首先获取了一个TextView对象,并且使用`getResources()`方法获取了一个Drawable对象。接下来,我们使用`setBounds()`方法来设置Drawable对象的大小和位置。最后,我们使用`setCompoundDrawables()`方法将Drawable对象与TextView相关联,这样就可以在TextView的左侧添加图片了。
需要注意的是,如果你想在TextView中添加多个图片,你需要使用`setCompoundDrawablesWithIntrinsicBounds()`方法,并传递四个参数,分别是左侧、顶部、右侧和底部的Drawable资源ID。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)