我正在尝试以编程方式创建ShapeDrawable,但以下代码没有显示任何内容。
ImageView image = new ImageView (context);
image.setLayoutParams (new LayoutParams (200, 200));
ShapeDrawable badge = new ShapeDrawable (new OvalShape());
badge.setBounds (0, 0, 200, 200);
badge.getPaint().setColor(Color.RED);
ImageView image = new ImageView (context);
image.setImageDrawable (badge);
addView (image);
我可以使用 XML 使其正常工作。
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size
android:width="200px"
android:height="200px" />
<solid
android:color="#F00" />
</shape>
ImageView image = new ImageView (context);
image.setLayoutParams (new LayoutParams (200, 200));
image.setImageResource (R.drawable.badge);
addView (image);
但我希望能够以编程方式创建它。XML 呈现得非常完美,因此问题肯定不在 ImageView 上,一定是在创建 ShapeDrawable 时出了问题。