android更改图像按钮背景

时间:2013-09-04 10:57:59

标签: android image button background

我似乎无法更改图像按钮的背景图像。下面是我正在尝试使用的代码:

ImageButton imgButton = (ImageButton) findViewById(R.id.showSportsButton);
imgButton.setBackgroundResource(R.drawable.tab2_selected);

然而,这似乎是将新图像放在旧图像的顶部,让我有2张图像相互重叠。

有谁知道这是为什么?

4 个答案:

答案 0 :(得分:10)

要解决这个问题,你应该实施

imgButton.setImageResource(R.drawable. tab2_selected);

答案 1 :(得分:1)

使用此方法:

imgButon.setBackground(getActivity().getDrawable(R.drawable.your_icon));

答案 2 :(得分:0)

在xml文件中,<Button>写:android:backgroud="@drawable/your_file"

答案 3 :(得分:0)

确保您的活动相同。如果要更改不同活动的背景,请先创建构造函数,然后使用object更改按钮。

Activity obj= new activity();
obj.imgButton.setBackgroundResource(R.drawable.tab2_selected);

如果您仅使用oncreate(),还会检查void方法是否有findviewbyid返回类型。

相关问题