Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

java - Items not showing in the ActionBar with showAsAction=“always”

I'm new to Android programming and currently trying to get the Actionbar working. My problem is, that although I set an item in the XML file to "always", it constantly ends up in the overflow menu no matter what I try. I found several similar problems on here, but none of the solutions fixed the problem.

Additional info: The ic_action_search icons are in the drawable folders of the project.

main_activity_actions.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<!-- Search, always displayed -->
<item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_search"
      android:title="@string/action_search"
      app:showAsAction="always" />


<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
      android:title="@string/action_settings"
      app:showAsAction="never" />
</menu>

strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

  <string name="app_name">My First App</string>
  <string name="edit_message">Enter a message</string>
  <string name="button_send">Send</string>
  <string name="action_settings">Settings</string>
  <string name="action_search">Search</string>
  <string name="title_activity_main">MainActivity</string>
  <string name="title_activity_display_message">My Message</string>
  <string name="hello_world">Hello world!</string>
</resources>

In MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

Thanks for your help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

try android:showAsAction instead of app:showAsAction. Or if you're using the appcompat_v7 backport, use both android:showAsAction and app:showAsAction(Thanks to Commonsware).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...