如何设置到code中的按钮背景图片如何设置、背景图片、按钮、code

2023-09-03 23:08:40 作者:跨过半个中国去睡你

我使用的使用创造了一个按钮下面code

I am using a button created using following code

LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);

Button btn = new Button(this);
btn.setOnClickListener(newtodobtn);
btn.setText("New Todo");

btn.setBackgroundDrawable(new Button(this).getBackground());

ll.addView(btn);

我在路径 @绘制/ new_todo_image 图片设置为背景的按钮。如何将其设置为按钮编程?

I have an image in path @drawable/new_todo_image to set as background for the button. How to set it to the button programmatically?

推荐答案

有关设置背景图片的按钮,这是在绘制的文件夹,然后用低于code

for set background image for button which is in drawable folder then use below code

btn.setBackgroundResource(R.drawable.new_todo_image);