Android 带有 app:showAsAction 的项目未显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26377305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Item with app:showAsAction not showing
提问by ITurchenko
I can't understand why wrong and incompatible (AndroidStudio tells me "Should use app:showAsAction with the appcompat library) code
我不明白为什么错误和不兼容(AndroidStudio 告诉我“应该将 app:showAsAction 与 appcompat 库一起使用)”代码
<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>
not showing my icon at all.
根本不显示我的图标。
I'm testing on Samsung GT P5210 (android v. 4.4.2) and Nexus 7 (4.4.4)
我正在三星 GT P5210 (android v. 4.4.2) 和 Nexus 7 (4.4.4) 上进行测试
回答by marduc812
Things you should always check when you want to use action bar are
当你想使用操作栏时你应该经常检查的事情是
1) Extend ActionBarActivity
instead of Activity
1)扩展ActionBarActivity
而不是Activity
public class MainMenu extends ActionBarActivity{
public class MainMenu extends ActionBarActivity{
2) Have the right styleselected as defined at manifest
2)根据清单中的定义选择正确的样式
Manifest
显现
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
Style
风格
<style name="AppTheme"
parent="Theme.AppCompat.Light.DarkActionBar">
</style>
3) Select the right titlefor showAsAction
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>
This is what most people get wrong
这是大多数人误会的地方
4) Define your Menu
in Activity
4)定义你Menu
的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.
如果您执行以下所有操作,您的操作栏应该可以工作。
Then you should add the onClickListener
for every position...
然后你应该onClickListener
为每个位置添加...
回答by blackfizz
I just reread your question and saw your problem is the complete opposite (but some pieces of my old answer still apply to your problem), so here is the updated answer:
我刚刚重读了你的问题,发现你的问题完全相反(但我的一些旧答案仍然适用于你的问题),所以这里是更新的答案:
Update:
更新:
You have imported the appcompat library in your gradle file, but you seem to support only devices newer than API Level 11 or 14? If this is the case, the lint check sees that you have imported the appcompat library via gradle and it thinks that you should use the ActionBarActivity
because of your library import. That's why you are getting the error. But as your android:showAsAction
attribute is working, you are using the native Activity
and the native attribute call is correct, even if the lint check says it is wrong. So if you want to remove the lint error, you have to delete the appcompat lib from your gradle file and maybe change your activity theme to the native Holo Light theme, as your current theme might rely on the appcompat theme.
您已经在 gradle 文件中导入了 appcompat 库,但您似乎只支持比 API 级别 11 或 14 更新的设备?如果是这种情况,lint 检查会发现您已经通过 gradle 导入了 appcompat 库,并且ActionBarActivity
由于您的库导入,它认为您应该使用。这就是您收到错误的原因。但是当您的android:showAsAction
属性正常工作时,您正在使用本机Activity
并且本机属性调用是正确的,即使 lint 检查表明它是错误的。因此,如果您想删除 lint 错误,您必须从您的 gradle 文件中删除 appcompat 库,并且可能将您的活动主题更改为原生 Holo Light 主题,因为您当前的主题可能依赖于 appcompat 主题。
The answer why it isn't working with the app
namespace is in the XML attribute loading for native respectively library code, which is handled in the old answer.
为什么它不与app
命名空间一起工作的答案是在本机和库代码的 XML 属性加载中,这是在旧答案中处理的。
Old Answer
旧答案
If you are using the ActionBarActivity
from the support library to reach devices lower than API level 11, the main issue here is, that the ActionBarActivity
recreates some of the native Android XML attributes such as android:showAsAction
in its own scope, which you define with:
如果您使用ActionBarActivity
支持库中的 来访问低于 API 级别 11 的设备,这里的主要问题是,ActionBarActivity
重新创建一些原生 Android XML 属性,例如android:showAsAction
在其自己的范围内,您定义为:
xmlns:app="http://schemas.android.com/apk/res-auto"
and then access them with the same attribute (here showAsAction
) in the app:
namespace.
So the ActionBarActivity can't see or reach the native android:showAsAction
attribute as it is only looking for it in the app
namespace and not the android
namespace.
然后使用命名空间中的相同属性(此处showAsAction
)访问它们app:
。因此 ActionBarActivity 无法看到或到达本机android:showAsAction
属性,因为它只是在app
命名空间中而不是在android
命名空间中寻找它。
If you want to use the native attribute, you have to use the native Activity
with a Holo Theme, which is only supported from API Level 11 and higher.
如果要使用 native 属性,则必须使用Activity
带有 Holo 主题的 native ,该主题仅从 API 级别 11 及更高版本支持。
回答by MilanNz
addthis:
补充一下:
yourapp:showAsAction="ifRoom"
or
或者
android:showAsAction
for example:
例如:
<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>
and in 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);
}
@Override
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_compose:
composeMessage();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And read morehere
在这里阅读更多