android textview drawabletop
时间: 2023-07-22 20:36:49 浏览: 49
`android:drawableTop` 是一个 TextView 的属性,它允许你在文本顶部添加一个图标。你可以在 XML 文件中使用它来设置 TextView 的属性,也可以在 Java 代码中使用 `setCompoundDrawablesRelativeWithIntrinsicBounds()` 方法来设置。
以下是一个示例 XML 代码:
```xml
<TextView
android:id="@+id/myTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:drawableTop="@drawable/my_icon" />
```
这会在 TextView 的顶部添加一个名为 `my_icon` 的图标。
如果你想在 Java 代码中设置这个属性,可以使用以下代码:
```java
TextView myTextView = findViewById(R.id.myTextView);
Drawable myIcon = getResources().getDrawable(R.drawable.my_icon);
myTextView.setCompoundDrawablesRelativeWithIntrinsicBounds(null, myIcon, null, null);
```
这会将 `my_icon` 图标设置为 TextView 的顶部图标。