简体   繁体   English

具有自己的动作栏项目的片段

[英]Fragments with own actionbar items

I've been searching for a few hours, and I can't get this work : 我已经搜寻了几个小时,却无法完成这项工作:

I have 2 fragments, I add a different menu item in each of them in the action bar. 我有2个片段,我在操作栏中的每个片段中添加了一个不同的菜单项。 在此处输入图片说明

It's working as you can see but what I want is that an item stays in his fragment "frame" (but still in the action bar) The items should be separated and stay on top of their own fragment like in the picture below. 如您所见,它正在工作,但是我想要的是一个项目保留在他的片段“框架”中(但仍在动作栏中)。这些项目应该分开并保留在它们自己的片段的顶部,如下图所示。

在此处输入图片说明

I've read I need to call setHasOptionsMenu in the method onCreate() of each fragment, but it still doesn't work as I'd like. 我读过我需要在每个片段的onCreate()方法中调用setHasOptionsMenu,但是它仍然无法按照我的意愿工作。

Here is my code : 这是我的代码:

MainActivity (hosting my 2 fragments) MainActivity(托管我的2个片段)

public class MainActivity extends SherlockFragmentActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Frag1 f1 = new Frag1();
    Frag2 f2 = new Frag2();

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.frag1, f1);
    ft.replace(R.id.frag2, f2);
    ft.commit();

   }

}

activity_main.xml activity_main.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"
tools:context=".MainActivity" >

     <LinearLayout
      android:id="@+id/frag1"
      android:layout_height="match_parent"
      android:layout_width="match_parent"
      android:layout_weight="1"
      >
     </LinearLayout>

     <LinearLayout
      android:id="@+id/frag2"
      android:layout_height="match_parent"
      android:layout_width="match_parent"
      android:layout_weight="1"
      >
     </LinearLayout>

</LinearLayout>

Frag1.java Frag1.java

public class Frag1 extends SherlockFragment{


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.frag_content, container, false);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    super.onCreateOptionsMenu(menu, inflater);
    menu.add(0, 12, 1, "Frag 1").setTitle("MENU ITEM 1").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);


   }

}

Frag2.java Frag2.java

public class Frag2 extends SherlockFragment{


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.frag_content2, container, false);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    super.onCreateOptionsMenu(menu, inflater);
    menu.add(5, 15, 101, "Frag 2").setTitle("MENU ITEM 2").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);


   }

}

The Layouts of the fragments are simple LinearLayout containing a TextView 片段的布局是包含TextView的简单LinearLayout

Could someone please point me to the right solution ? 有人可以指出正确的解决方案吗?

The items should be separated and stay on top of their own fragment like in the picture below. 这些项目应分开放置,并留在自己的片段顶部,如下图所示。

That is not supported and goes against the Android design guidelines for the action bar . 不支持该功能,并且违反了操作栏的Android设计准则 Also, watch the Android Design in Action video where Google engineers specifically point out these sorts of action bar problems (at around the 14:10 mark). 另外,请观看Android Design in Action视频 ,其中Google工程师专门指出了这类操作栏问题(大约14:10标记)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM