如何在android中通过java设置android:drawableTop="@drawable/your_drawable_selector"?

how to set android:drawableTop="@drawable/your_drawable_selector" through java in android?

如何设置

 android:drawableTop="@drawable/your_drawable_selector" 

到 java 在 android?

中的 RadioButton

您可以使用 setCompoundDrawablesWithIntrinsicBounds,因为 RadioButton 是 TextView 的子类。例如。

radioButton.setCompoundDrawablesWithIntrinsicBounds (0, R.drawable.your_drawable_selector, 0, 0).

如果你有位图,你可以使用

Drawable drawable =  new BitmapDrawable(getResources(), yourBitmap);
radioButton.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null); 

来自文档

Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.