【背景】
android的app中,原先是:
即:
对应的是标题栏(actionbar,titlebar)中,菜单按钮是文字,
想要换成图标。
注:
此处对应的配置是:
xml的配置:
res\menu\field_view_menu.xml
的:
1 2 3 4 5 6 7 8 9 | < item android:id = "@+id/save_edited_fields" android:orderInCategory = "10" android:showAsAction = "always" android:title = "@string/save" /> </ menu > |
和:
res\menu\file_item.xml
的:
1 2 3 4 5 6 7 8 9 | < item android:id = "@+id/action_settings" android:orderInCategory = "100" android:showAsAction = "never" android:title = "@string/action_settings" /> </ menu > |
对应的文字内容是:
res\values\strings.xml
的:
1 2 3 4 5 6 7 | <? xml version = "1.0" encoding = "utf-8" ?> < resources > ...... < string name = "save" >Save</ string > < string name = "setting" >Setting</ string > ...... </ resources > |
【解决过程】
1.参考:
去把android:title换成android:icon
改为:
1 2 3 4 5 6 7 8 9 10 | < item android:id = "@+id/action_settings" android:orderInCategory = "100" android:showAsAction = "never" android:title = "@string/action_settings" android:icon = "@drawable/settings" /> </ menu > |
看看效果。
不行。还是文字。
2.参考:
ActionBar Action Items not showing
加上:
1 2 3 4 5 6 7 8 9 10 11 12 13 | xmlns:app="http://schemas.android.com/apk/res-auto" 变成: < item android:id = "@+id/action_settings" android:orderInCategory = "100" android:showAsAction = "never" android:title = "@string/action_settings" android:icon = "@drawable/settings" /> </ menu > |
看看效果。结果问题依旧。
3.不过,貌似改错xml文件了?
所以去改:
res\menu\common_status_view_menu.xml
为:
1 2 3 4 5 6 7 8 9 | <? xml version = "1.0" encoding = "utf-8" ?> < item android:id = "@+id/setting_menu" android:orderInCategory = "100" android:showAsAction = "ifRoom" android:title = "@string/setting" android:icon = "@drawable/settings" /> </ menu > |
试试。
结果是可以的。
4.再去修改另外的:
res\menu\field_save_status_view_menu.xml
为:
1 2 3 4 5 6 7 8 9 | <? xml version = "1.0" encoding = "utf-8" ?> < item android:id = "@+id/save_edited_fields" android:orderInCategory = "100" android:showAsAction = "ifRoom" android:title = "@string/save" android:icon = "@drawable/save" /> </ menu > |
结果是:
可以正确显示了:
【总结】
把之前的xml中的menu,本身带了:
android:title="@string/save"
再添加上对应的:
android:icon="@drawable/save"
再去添加对应的
res/drawable/save.png
即可。
注:
1.对于别的图片,比如另外那个settings,也是同样操作即可。
2.貌似添加jpg图片,不支持。
估计是
res/drawable
只支持png,而不支持jpg图片。