简体   繁体   English

以编程方式更改Android Drawable Button图标

[英]Change Android Drawable Button Icon Programmatically

How would one change the android:src XML property programatically of a FloatingActionButton or Button widget? 如何以编程方式更改FloatingActionButton或Button小部件的android:src XML属性? I have a button defined as following within an activity: 我在活动中定义了一个按钮:

FloatingActionButton floatingActionButton = (FloatingActionButton) this.findViewById(R.id.start_button);

It has an initial android:src set as follows: 它有一个初始的android:src设置如下:

android:src="@android:drawable/ic_media_play"

I'm trying to utilize the setImageResource(int id) to change the android:src XML property, but how to I access a Drawable icon such as ic_media_pause after a button click that occurs for example. 我正在尝试利用setImageResource(int id)来更改android:src XML属性,但是如何在按下单击按钮后访问一个Drawable图标,例如ic_media_pause When I try to pass in R.drawable.ic_media_pause to the setImageResource() method I received an cannot resolve symbol error. 当我尝试将R.drawable.ic_media_pause传递给setImageResource()方法时,我收到了一个无法解决的符号错误。

floatingActionButton.setImageResource(R.drawable.ic_media_pause);

How do I get at the available R.drawable resources to pass it in as an argument. 如何获取可用的R.drawable资源以将其作为参数传递。

Any help would be great appreciated. 任何帮助将非常感谢。 Thank you! 谢谢!

You're trying to access a default icon included in the SDK. 您正尝试访问SDK中包含的默认图标。 You need to prefix your resource identifier with "android." 您需要在资源标识符前加上“android”。 For example: 例如:

floatingActionButton.setImageResource(android.R.drawable.ic_media_pause); 

this will allow you to reference the default icons. 这将允许您引用默认图标。

:) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM