drawableTop
时间: 2023-11-24 08:38:45 浏览: 24
drawableTop是Android中TextView、Button等控件提供的一个属性,用于设置控件中文本的上方显示一个图片。可以通过在xml中设置drawableTop属性的值来设置图片,也可以通过代码动态设置。例如:
在xml中设置:
```xml
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button Text"
android:drawableTop="@drawable/ic_launcher" />
```
通过代码设置:
```java
Button btn = findViewById(R.id.btn);
Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);
btn.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
```
其中,setCompoundDrawablesWithIntrinsicBounds()方法用来设置控件中文本的上方图片,第一个参数为左边图片,第二个参数为上方图片,第三个参数为右边图片,第四个参数为下方图片。通过传入null值来表示不需要设置的方向。
相关问题
drawabletop
drawableTop是TextView中的一个属性,它可以在TextView的文本上方设置一个drawable图像。除了drawableTop之外,还有drawableLeft、drawableRight和drawableBottom等属性,它们分别可以在TextView的文本左侧、右侧和下方设置drawable图像。这些属性可以通过在布局文件中设置或者在代码中动态设置来实现。在代码中动态设置drawableTop可以使用setCompoundDrawables方法,也可以重写setCompoundDrawablesWithIntrinsicBounds方法来设定图片的大小。
android textview drawabletop
`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 的顶部图标。