AppCompat_v7工具栏的动作条不显示,从菜单中选择“总是”的行动,但API的工具栏上 [英] AppCompat_v7 Toolbar as actionbar not showing 'always' actions from menu, but API Toolbar does

查看:105
本文介绍了AppCompat_v7工具栏的动作条不显示,从菜单中选择“总是”的行动,但API的工具栏上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在2天新的API 21工具栏和appCompat_v7的有点吃力,我想我发现了一个错误就可以了。如果你有你的菜单上2的操作是这样的:

After 2 days of stuggling 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:

<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"/>

和这样定义的appCompat工具栏:

and a appCompat toolbar defined like this:

<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>

膨胀(或设置setSupportActionBar法)后

after inflating (or setting the setSupportActionBar method)

Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
toolbar.setTitle("esurance");
setSupportActionBar(toolbar);

您将得到的工具栏菜单中的您的操作图标,就会溢出菜单上显示出来。

you will get the toolbar menu without your action icon, it will display it on the overflow menu.

不过,如果从21 API使用工具栏类,它会为你的菜单布局定义显示你的行动......

But, if you use the Toolbar class from API 21, it will show your actions as defined on your menu layout...

<Toolbar 
    android:layout_width="match_parent"
    android:layout_height="52dp"
    android:id="@+id/toolbar">
</Toolbar>

也许我失去了一些东西,但到目前为止,我已经无法显示使用appCompat溢出菜单以外的行动。任何帮助将大大AP preciated。

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.

推荐答案

按照操作栏训练,你必须使用应用:showAsAction 属性,而不是安卓showAsAction 属性:

Per the Action Bar training, you have to use the app:showAsAction attributes rather than the android:showAsAction attribute:

请注意,showAsAction上面的属性将使用在中所定义自定义命名空间;菜单&gt; 标记。使用由支持库定义的XML属性时,因为这些属性在旧设备的Andr​​oid框架是不存在的,这是必要的。所以,你必须使用自己的名称空间为preFIX由支持库中定义的所有属性。

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.

所以,你的菜单文件应该是这样的:

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>

这篇关于AppCompat_v7工具栏的动作条不显示,从菜单中选择“总是”的行动,但API的工具栏上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆