TextView的setCompoundDrawablesRelative和setCompoundDrawables区别
setCompoundDrawablesRelative() 和 setCompoundDrawables() 都是用于在 TextView(或其子类)中设置文本的绘制图标(Compound Drawables)的方法。它们之间的区别在于绘制图标的位置和方向。
setCompoundDrawablesRelative():
该方法是从 Android 4.2(API 级别 17)引入的。
在设置文本的同时,可以指定绘制图标相对于文本的位置和方向。
使用相对位置的常量参数:START、TOP、END、BOTTOM。
默认情况下,文本的方向会影响图标的方向,比如在从右到左的语言环境中,START 将在文本的右侧绘制图标,END 将在文本的左侧绘制图标。
示例:
textView.setCompoundDrawablesRelative(startDrawable, topDrawable, endDrawable, bottomDrawable);
setCompoundDrawables():
该方法是早期版本的 API 提供的。
只能设置固定的绘制图标的位置,不考虑文本的方向。
使用绝对位置的常量参数:LEFT、TOP、RIGHT、BOTTOM。
示例:
textView.setCompoundDrawables(leftDrawable, topDrawable, rightDrawable, bottomDrawable);
- 总而言之