为什么 `ifroom` 不起作用 - android studio

why `ifroom` does not work - android studio

你好,我尝试在我的菜单项中设置 ifroom。你可以在这段代码中看到我尝试了所有可能的 senario(请查看每个 showAsAction 项目

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">

    <item
        android:id="@+id/action_forward"

        android:title="forward"
        android:icon="@drawable/ic_action_arrow_forward"
        android:showAsAction="ifRoom"
        ></item>


    <item
        android:id="@+id/action_zoom_in"
        android:title="zoom in"
        android:icon="@drawable/ic_action_zoom_in"
        android:showAsAction="ifRoom|withText"
        ></item>



    <item
        android:id="@+id/action_home"
        android:title="home"
        android:icon="@drawable/ic_action_home"
        app:showAsAction="ifRoom|withText"
        ></item>


    <item
        android:id="@+id/action_refresh"
        android:title="refresh"
        android:icon="@drawable/ic_action_refresh"
        app:showAsAction="ifRoom"
        ></item>

    <item
        android:id="@+id/action_zoom_out"
        android:title="zoom out"
        android:icon="@drawable/ic_action_zoom_out"
        showAsAction="ifRoom"
        ></item>

    <item
        android:id="@+id/action_back"
        android:title="back"
        android:icon="@drawable/ic_action_arrow_back"
        app:showAsAction="always"
        ></item>
</menu>

但它只在主页显示 action_backaction_home

我是 android 工作室的菜鸟,但我认为这个错误与菜单的宽度有关。 (不是 100% 宽度)

请看我的main_activity.xml

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity">


    <include
        android:id="@+id/tool_bar_bottom"
        layout="@layout/tool_bar_bottom"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_gravity="center" />

</LinearLayout>

tool_bar_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:background="@color/ColorPrimary"
    android:elevation="2dp"
    android:theme="@style/Base.ThemeOverlay.AppCompat.Dark"
    xmlns:android="http://schemas.android.com/apk/res/android" />

我认为结构是正确的,所有 layout_width 的值都是 match_parent 那么为什么 ifroom 对我不起作用?

如果将 "showAsAction" 属性设置为 "always",

Android 将始终显示菜单。如果你设置 "ifRoom",只有当 actionbar 有空间显示你的菜单项时才会显示,否则它会显示一个溢出图标。

在您的例子中,您看到的是 "action_back",因为它有 "always"。然后 "action_home" 因为有一个图标的空间,这是第一个具有 "app:showAsAction" 属性的项目。其他人有 "android:showAsAction" 属性。有关此行为的更多解释如下。

If your app is using the Support Library for compatibility on versions as low as Android 2.1, the showAsAction attribute is not available from the android: namespace. Instead this attribute is provided by the Support Library and you must define your own XML namespace and use that namespace as the attribute prefix. (A custom XML namespace should be based on your app name, but it can be any name you want and is only accessible within the scope of the file in which you declare it.)