Android AppCompat_v7 工具栏作为操作栏不显示菜单中的“始终”操作,但 API 工具栏显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26638089/
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
AppCompat_v7 Toolbar as actionbar not showing 'always' actions from menu, but API Toolbar does
提问by Roisgoen
After 2 days of struggling with the new API 21 Toolbar and appCompat_v7, I think I found a bug on it. If you have 2 actions on your menu like this:
在与新的 API 21 工具栏和 appCompat_v7 苦苦挣扎 2 天之后,我想我发现了一个错误。如果您的菜单上有 2 个操作,如下所示:
<item
android:id="@+id/action_test"
android:showAsAction="always"
android:icon="@drawable/ic_launcher"
android:title="@string/action_settings"/>
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_settings"/>
and a appCompat toolbar defined like this:
和一个 appCompat 工具栏定义如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.toolbar.MainActivity" >
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="52dp"
android:id="@+id/toolbar">
</android.support.v7.widget.Toolbar>
</RelativeLayout>
after inflating (or setting the setSupportActionBar method)
充气后(或设置 setSupportActionBar 方法)
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
toolbar.setTitle("esurance");
setSupportActionBar(toolbar);
you will get the toolbar menu withoutyour action icon, it will display it on the overflow menu.
您将获得没有操作图标的工具栏菜单,它将显示在溢出菜单上。
But, if you use the Toolbar class from API 21, it will show your actions as defined on your menu layout...
但是,如果您使用 API 21 中的 Toolbar 类,它将显示您在菜单布局中定义的操作...
<Toolbar
android:layout_width="match_parent"
android:layout_height="52dp"
android:id="@+id/toolbar">
</Toolbar>
Maybe I'm missing something here, but so far, I've been unable to display actions outside the overflow menu using appCompat. Any help on this will be much appreciated.
也许我在这里遗漏了一些东西,但到目前为止,我无法使用 appCompat 在溢出菜单之外显示操作。对此的任何帮助将不胜感激。
回答by ianhanniballake
Per the Action Bar training, you have to use the app:showAsAction
attributes rather than the android:showAsAction
attribute:
根据Action Bar training,您必须使用app:showAsAction
属性而不是android:showAsAction
属性:
Notice that the showAsAction attribute above uses a custom namespace defined in the
<menu>
tag. This is necessary when using any XML attributes defined by the support library, because these attributes do not exist in the Android framework on older devices. So you must use your own namespace as a prefix for all attributes defined by the support library.
请注意,上面的 showAsAction 属性使用
<menu>
标记中定义的自定义命名空间。当使用支持库定义的任何 XML 属性时,这是必要的,因为这些属性在旧设备上的 Android 框架中不存在。因此,您必须使用自己的命名空间作为支持库定义的所有属性的前缀。
So your menu file should look 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_test"
app:showAsAction="always"
android:icon="@drawable/ic_launcher"
android:title="@string/action_settings"/>
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
app:showAsAction="never"
android:title="@string/action_settings"/>
</menu>
回答by Singhak
put
放
xmlns:app="http://schemas.android.com/apk/res-auto" int your menu tag
xmlns:app="http://schemas.android.com/apk/res-auto" 输入你的菜单标签
like this
像这样
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
then use app:showAsAction = "always"
然后使用 app:showAsAction = "always"
Remember that use
app:showAsAction
in all menu item notandroid:showAsAction
请记住,
app:showAsAction
在所有菜单项中不使用android:showAsAction
`
`
回答by ShahinFasihi
first of all use the app:showAsAction
instead of android:showAsAction
(like @ianhanniballake)
after that in onCreateActionMode
after Inflating your menu setShowAsAction
in code like this
首先使用app:showAsAction
而不是android:showAsAction
(如@ianhanniballake)然后
在像这样的代码中onCreateActionMode
充气你的菜单之后setShowAsAction
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.your_menu_name, menu);
menu.findItem(R.id.your_first_menu_id).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.findItem(R.id.your_second_menu_id).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
i had the same problem so after working on it these lines of code works fine for me.
我遇到了同样的问题,所以在处理它之后,这些代码行对我来说很好用。