我正在尝试以编程方式创建一个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.
ImageView image = new ImageView (context); image.setLayoutParams (new LayoutParams (200, 200)); image.setImageResource (R.drawable.badge); addView (image);
但我想以编程方式创建它.xml工作得很好所以问题不能用于ImageView,它必须在创建ShapeDrawable.
使用setIntrinsicWidth
而setIntrinsicHeight
不是setBounds
设置宽度和高度.
ImageView image = new ImageView (context); image.setLayoutParams (new LayoutParams (200, 200)); ShapeDrawable badge = new ShapeDrawable (new OvalShape()); badge.setIntrinsicWidth (200); badge.setIntrinsicHeight (200); badge.getPaint().setColor(Color.RED); image.setImageDrawable (badge); addView (image);