java - showAsAction ="always"在双 Pane 模式下不起作用

标签 java android android-layout android-fragments android-menu

我尝试使用 app:showAsAction="always"将我的应用程序中的菜单项设置为操作。当我使用相同的布局时,这适用于手机和平板电脑。

但是当我使用双 Pane 布局时,即使有足够的空间,项目也不会显示在操作栏中,而是显示在溢出菜单中。

这是菜单 xml:

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

    <item
        android:id="@+id/menu_item_maps"
        android:icon="@drawable/ic_action_place"
        android:orderInCategory="20"
        android:title="@string/maps_menu_item"
        app:showAsAction="always">
    </item>
    <item
        android:id="@+id/menu_item_event"
        android:icon="@drawable/ic_action_event"
        android:orderInCategory="30"
        android:title="@string/menu_item_event"
        app:showAsAction="always">
    </item>
    <item
        android:id="@+id/menu_item_share_gig"
        android:orderInCategory="100"
        android:title="@string/menu_item_share"
        app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
        app:showAsAction="always">
    </item>

</menu>

这是双 Pane 布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:baselineAligned="false"
    android:divider="?android:attr/dividerHorizontal"
    android:orientation="horizontal"
    android:showDividers="middle"
     >

    <fragment
        android:id="@+id/gig_list"
        android:name="de.nobodyknows.app.GigListFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

    <FrameLayout
        android:id="@+id/gig_detail_container"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />

</LinearLayout>

在我的代码中,我使用回调方法将 FrameLayout 替换为详细 fragment :

/**
 * Callback method from {@link GigListFragment.Callbacks} indicating that
 * the item with the given ID was selected.
 */
@Override
public void onItemSelected(Long id) {
    if (mTwoPane) {
        // In two-pane mode, show the detail view in this activity by
        // adding or replacing the detail fragment using a
        // fragment transaction.
        Bundle arguments = new Bundle();
        arguments.putLong(GigDetailFragment.GIG_ID, id);
        GigDetailFragment fragment = new GigDetailFragment();
        fragment.setArguments(arguments);
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.gig_detail_container, fragment).commit();

    } else {
        // In single-pane mode, simply start the detail activity
        // for the selected item ID.
        Intent detailIntent = new Intent(this, GigDetailActivity.class);
        detailIntent.putExtra(GigDetailFragment.GIG_ID, id);
        startActivity(detailIntent);
    }
}

详细信息 Fragment 扩展了上面显示的选项菜单:

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.detail, menu);
    }

现在,当开始新的 Activity 时,这些菜单项可以很好地显示在手机布局中。但是当 fragment 替换第一个 Activity 中的 FrameLayout 时,菜单项只会显示在溢出菜单中。不过,我不明白为什么会这样。它与支持库有关吗?还是 fragment 的工作方式有什么特别之处?

感谢您的帮助。

最佳答案

好的,我找到了解决方案。

我的 Activity 没有扩展 ActionBarActivity因此未正确设置为与 MenuItemCompat 一起使用。 它在手机上仍然有效的原因是因为它位于另一个正确扩展 ActionBarActivity 的 Activity 中。

为了更正这个问题,我只是将充当双 Pane Activity 的 Activity 的父类(super class)替换为 ActionBarActivity

关于java - showAsAction ="always"在双 Pane 模式下不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25666712/

相关文章:

Android 将数据返回到之前的 Activity

java - 如何并排调整两个 TextView 的大小并使用屏幕宽度的 50%

android - 如何从aws s3下载图片到imageview?

Android 在线性布局上的大小相等

java - Android AlertDialog 设置项目对齐中心

android - 创建 Google 音乐专辑甲板自定义 ListView

java Nested If 或 single if

java - 尝试在 Android 中打开新屏幕/Activity

java - Hibernate:如何根据作为关联映射一部分的外键列选择单个项目?

java - 有时,确实会创建唯一 id 的消费者组,并且消费者会在没有分区的情况下卡住