【问题标题】:change text to image and back on button at runtime in Android在Android运行时将文本更改为图像并返回按钮
【发布时间】:2013-03-27 17:11:43
【问题描述】:

我想在运行时将按钮的文本更改为图像,然后在某些用户交互时返回(而不是我想要文本返回的图像)。 我怎样才能做到这一点?谁能给我举个例子?

据我了解,我无法使用图像按钮,因为我无法在其上添加文本。 我试过用

setCompoundDrawables

但这对我不起作用(按钮上没有可见的变化)。这是我的代码:

 Button button = (Button) findViewById(R.id.button1);

 Drawable myDrawable = this.getResources().getDrawable(R.drawable.ic_launcher);
 myDrawable.setBounds(0, 0, 0, 0);

 button.setCompoundDrawables(myDrawable, null, null, null);

请帮忙

【问题讨论】:

    标签: android button


    【解决方案1】:

    我认为这是问题

    myDrawable.setBounds(0, 0, 0, 0);
    

    由于您为left, top, right, bottom 提供了0,因此没有明显的变化。尝试为rightbottom 提供一些值,例如1015,您将能够看到变化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-09
      • 1970-01-01
      • 1970-01-01
      • 2021-08-09
      • 2012-07-07
      相关资源
      最近更新 更多