【问题标题】:how to replace/change image button programmatically android如何以编程方式替换/更改图像按钮android
【发布时间】:2013-05-13 15:46:24
【问题描述】:

我的视图上有一个图像按钮,我需要在用户交互后更改它。我找不到像 myImageButton.setDrawable 这样的东西,这是我要更改的按钮的 xml:

<ImageButton
        android:id="@+id/stopButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/buttons_background"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dp"
        android:background="@null"
        android:src="@drawable/btn_play" />

我需要以编程方式将android:src="@drawable/btn_play" 更改为android:src="@drawable/btn_stop" 的唯一行。这可以做到吗?提前致谢。

【问题讨论】:

    标签: java android android-layout android-button


    【解决方案1】:

    你可以使用

    setImageResource for the image button
    

    【讨论】:

      【解决方案2】:

      使用这个:

      myImageButton.setImageResource(R.drawable.btn_stop);
      

      【讨论】:

        【解决方案3】:
        ImageButton = (ImageButton)findViewById(R.id.stopButton);
        btn.setCompoundDrawablesWithIntrinsicBounds(R.drawable.btn_stop, 0, 0, 0);
        

        【讨论】:

        • 谢谢!适用于按钮。
        【解决方案4】:

        使用

        setImageResource(R.drawable.btn_stop);

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-05-14
          • 2011-03-07
          相关资源
          最近更新 更多