我想以编程方式创建一个自定义绘制形状的按钮作为图像。在我的活动onCreate中,我有:
final FrameLayout contentContainer = new FrameLayout(this);
final FrameLayout.LayoutParams contentLayoutParams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.FILL_PARENT,
FrameLayout.LayoutParams.FILL_PARENT);
contentContainer.setLayoutParams(contentLayoutParams);
contentContainer.setBackgroundColor(Color.BLUE);
int size = 50;
final ShapeDrawable drawable = new ShapeDrawable(new OvalShape());
drawable.getPaint().setColor(Color.RED);
drawable.setBounds(0, 0, size, size);
final ImageButton leftArrow = new ImageButton(this);
leftArrow.setImageDrawable(drawable);
final FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(size, size);
leftArrow.setLayoutParams(lp);
contentContainer.addView(leftArrow);
当我运行应用程序时,我看到一个带有纯白色按钮的蓝屏。我希望在按钮上看到一个红色的椭圆形,但似乎我不理解setImageDrawable调用。
有任何建议如何实现这一目标?谢谢!