Android 复合可绘制对象

Android compound drawable

我正在使用自定义 toast,ImageViewTextView 使用可绘制背景。但是 Android Studio 告诉我改用一个 TextView 和一个 复合可绘制对象 。我已经使用了一个 drawable 作为背景,但是我如何 "draw" 相对于 TextView 的特定位置的图片?

您可以像这样使用一个 TextView 和一个 CompoundDrawable

Toast toast = Toast.makeText(this, "Custom Compount Drawable Toast", Toast.LENGTH_LONG);
View toastView = toast.getView(); //This'll return the default View of the Toast.

TextView toastMessage = (TextView) toastView.findViewById(android.R.id.textview);
toastMessage.setTextSize(25);
toastMessage.setCompoundDrawablesWithIntrinsicBounds(R.drawable.image_id, 0, 0, 0);
toastMessage.setGravity(Gravity.CENTER);
toastMessage.setCompoundDrawablePadding(16);
toastView.setBackgroundColor(Color.CYAN);
toast.show();