gpt4 book ai didi

java - Android添加操作按钮问题

转载 作者:太空宇宙 更新时间:2023-11-04 15:10:46 25 4
gpt4 key购买 nike

我尝试在操作栏上添加两个操作按钮,为了可见性,其中一个定义为:

android:showAsAction="ifRoom"

另一个定义为:

android:showAsAction="never"

问题是我可以看到 ic_action_search 图标,但看不到 ic_action_overflow 图标。这是 menu 文件夹中的 main_activity_actions.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:MyFirstApp="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
MyFirstApp:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
android:icon="@drawable/ic_action_overflow"
android:title="@string/action_settings"
MyFirstApp:showAsAction="never" />

</menu>

这是包含按钮的 Java 代码:

   @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity_actions, menu);

return super.onCreateOptionsMenu(menu);
//return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
// openSearch();
return true;
case R.id.action_settings:
// openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}

}

那么我的代码有什么问题吗?

干杯

最佳答案

<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=".MainActivity">
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
app:showAsAction="ifRoom" />
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
</menu>

这是工作

关于java - Android添加操作按钮问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21356378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com