java–Android无法在选项菜单中添加搜索按钮

java–Android无法在选项菜单中添加搜索按钮,第1张

概述我正在关注这个Tutorial但是我无法在选项菜单上看到一个简单的搜索按钮.码:main_activity_actions.xml<menuxmlns:android="http://schemas.android.com/apkes/android"><!--Search,shouldappearasactionbutton--><itemandroid:id="@+id/action_s

我正在关注这个Tutorial 但是我无法在选项菜单上看到一个简单的搜索按钮.
码:

main_activity_actions.xml

<menu xmlns:androID="http://schemas.androID.com/apk/res/androID" >    <!-- Search, should appear as action button -->    <item androID:ID="@+ID/action_search"          androID:icon="@drawable/ic_action_search"          androID:title="@string/action_search"          androID:showAsAction="ifRoom" />    <!-- Settings, should always be in the overflow -->    <item androID:ID="@+ID/action_settings"          androID:title="@string/action_settings"          androID:showAsAction="never" /> </menu>

将此添加到MainActivity.java

public boolean onCreateOptionsMenu(Menu menu) {    // Inflate the menu; this adds items to the action bar if it is present.    MenuInflater inflater = getMenuInflater();    inflater.inflate(R.menu.main_activity_actions, menu);    return super.onCreateOptionsMenu(menu);    }

应用程序加载正常但没有显示图标,我已下载标准图标并将它们放在每个可绘制文件夹中.任何想法为什么它不起作用?

不确定是否相关,但教程说明

“图标属性需要图像的资源ID.@ drawable /后面的名称必须是您在项目的res / drawable /目录中保存的位图图像的名称.例如,”@ drawable / ic_action_search“指的是to ic_action_search.png.“

但我没有文件夹res / drawable /只有res / drawable-hdpi,res / drawable-ldpi …
Shold我创建另一个文件夹res / drawable /如果是这样的图标应该进去(我下载的官方图标集有所有res / drawable-hdpi但没有res / drawable /)

解决方法:

只需用我的代码替换你的代码,它就会对你有用.

<menu xmlns:androID="http://schemas.androID.com/apk/res/androID"        xmlns:app="http://schemas.androID.com/apk/res-auto"        xmlns:tools="http://schemas.androID.com/tools"        tools:context="com.example.actionbar.MainActivity" >         <item androID:ID="@+ID/action_search"              androID:icon="@drawable/ic_action_search"              androID:title="@string/action_search"              app:showAsAction="always"/>          <item androID:ID="@+ID/action_settings"              androID:title="@string/action_settings"              app:showAsAction="never"/>    </menu>
总结

以上是内存溢出为你收集整理的java – Android无法在选项菜单中添加搜索按钮全部内容,希望文章能够帮你解决java – Android无法在选项菜单中添加搜索按钮所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1099246.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存