Android:创建图像按钮,以编程方式绘制的形状作为其图像

Android:创建图像按钮,以编程方式绘制的形状作为其图像,android,Android,我想以编程方式创建一个按钮,以自定义绘制的形状作为图像。在我的活动onCreate中,我有: final FrameLayout contentContainer = new FrameLayout(this); final FrameLayout.LayoutParams contentLayoutParams = new FrameLayout.LayoutParams( FrameLayout.LayoutParams.FILL_PARENT, FrameL

我想以编程方式创建一个按钮,以自定义绘制的形状作为图像。在我的活动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调用


如何实现这一目标,有什么建议吗?谢谢

如果您尝试将drawable设置为按钮的背景,而不是图像,使用setBackgroundDrawable,会发生什么情况?是的,看起来很有效-谢谢!如果你想让它成为一个答案,我想我可以接受它…如果你尝试将可绘制按钮设置为按钮的背景而不是图像,那么会发生什么?是的,看起来很有效-谢谢!如果你想回答,我想我可以接受。。。