问题描述
我不明白为什么错误和不兼容(AndroidStudio 告诉我应该使用 app:showAsAction 和 appcompat 库)"代码
I can't understand why wrong and incompatible (AndroidStudio tells me "Should use app:showAsAction with the appcompat library) code
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/action_search"
android:title="@string/action_search"
android:icon="@drawable/search"
android:showAsAction="always" />
</menu>
工作完美,但正确且兼容的版本如
works perfect, but proper and compatible version like
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item android:id="@+id/action_search"
android:title="@string/action_search"
android:icon="@drawable/search"
app:showAsAction="always" />
</menu>
根本不显示我的图标.
我正在三星 GT P5210 (android v. 4.4.2) 和 Nexus 7 (4.4.4) 上进行测试
I'm testing on Samsung GT P5210 (android v. 4.4.2) and Nexus 7 (4.4.4)
推荐答案
当你想使用操作栏时你应该经常检查的事情是
Things you should always check when you want to use action bar are
1) 扩展 ActionBarActivity
而不是 Activity
1) Extend ActionBarActivity
instead of Activity
公共类 MainMenu 扩展 ActionBarActivity{
2) 选择在清单中定义的正确样式
2) Have the right style selected as defined at manifest
清单
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
风格
<style name="AppTheme"
parent="Theme.AppCompat.Light.DarkActionBar">
</style>
3) 为showAsAction
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:**yourapp**="http://schemas.android.com/apk/res-auto" >
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
**yourapp**:showAsAction="ifRoom" />
...
</menu>
这是大多数人的错误
4) 在 Activity
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
如果您执行以下所有操作,您的操作栏应该可以工作.
If you do all the following your action bar should work.
然后你应该为每个位置添加onClickListener
...
Then you should add the onClickListener
for every position...
这篇关于带有 app:showAsAction 的项目未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!