![](/img/trans.png)
[英]Is it possible to change material design icon color from xml in Android?
[英]How to change color of material design icon used in Button in Android
我的XML中有这个Button
:
<Button
android:id="@+id/button_message_me"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="0dp"
android:layout_weight="0.25"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="@drawable/ic_chat_white_48dp"
android:onClick="clickMessageMe"/>
但是,我想将ic_chat_white_48dp
的颜色从白色更改为蓝色。 怎么做改变?
到目前为止,我已经尝试过:
看完这篇文章后,我尝试使用android:tint="@color/blue"
但是没有用。
因此,以为我可能需要使用ImageButton
而不是Button
(如答案中所述),所以我在XML中用ImageButton
替换了Button
,但是我发现出现了异常:
java.lang.RuntimeException:无法启动活动ComponentInfo {chat.knowme.knowme / chat.knowme.knowme.ShowProfileActivity}:java.lang.ClassCastException:android.support.v7.widget.AppCompatImageButton无法转换为android.widget。纽扣
任何帮助将非常感激。 非常感谢!
更新:崩溃是因为我在源代码中将ImageButton
投射为Button
。 我已修复它,现在不再崩溃了(感谢Patel Pinkal的回答)。
但是,即使使用ImageButton
,颜色仍然保持不变
我也面临这个问题。 最后,使用Android Material Design图标生成器插件找到了解决方案。 通过此操作,您可以生成各种颜色和大小的图标。
步骤1:“文件”->“设置”->“插件”
步骤2:搜索android material icon,您将在列表中获得Android Material Design Icon Generator插件。
步骤3:选择该选项,然后单击“应用”
安装完成后,从studio-> new-> Material design图标或ctrl + alt + M右键单击您的项目
这将对开发人员有用。
您正在尝试初始化Button
而不是AppCompatImageButton
。 像这样替换
AppCompatImageButton appCompatImageButton = (AppCompatImageButton) findViewById(R.id.appCompatImageButton);
代替 :
Button button = (Button) findViewById(R.id.button);
和
如果要使用Button
或AppCompatImageButton
更改颜色,则必须像
android:backgroundTint="@Color/yourColor"
代替
android:tint="@Color/yourColor"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.