【问题标题】:android change image button backgroundandroid更改图像按钮背景
【发布时间】:2013-09-07 20:22:18
【问题描述】:

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

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

但是,这似乎是将新图像放在旧图像之上,让我有 2 张图像相互重叠。

有人知道这是为什么吗?

【问题讨论】:

  • 我猜你必须使用继承自ImageView.setImageResource
  • please add imgButton.setImageResource(null); imgButton.setImageResource(R.drawable. tab2_selected);
  • 什么是旧图像 - 默认图像,表示灰色(例如,取决于主题)图像?

标签: android image button background


【解决方案1】:

为了解决这个问题,你应该实现

imgButton.setImageResource(R.drawable. tab2_selected);

【讨论】:

    【解决方案2】:

    使用这个方法:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

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

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

        【讨论】:

          猜你喜欢
          • 2016-07-12
          • 2018-03-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-19
          • 2020-11-13
          相关资源
          最近更新 更多