2016-07-11 10 views
2

주 활동 메뉴 및 주 활동에 포함 된 조각 메뉴를 갖고 싶습니다. 프래그먼트가 표시되면 주 작업 메뉴가 제거되고 조각 메뉴가 비정상적으로 표시됩니다. 나는 그러나이 결과이를 달성하기 위해 노력했다 : 그 자체로활동에서와 다른 메뉴를 사용하는 방법은 무엇입니까?

주요 활동 메뉴 조각 : 조각이 표시됩니다

enter image description here

주요 활동 메뉴 조각 :

enter image description here

분명히 주요 액티비티의 메뉴를 대체하는 대신 조각 메뉴를 만들고 부풀려도 추가하고 있습니다.

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

    <item 
    android:icon="@drawable/ic_person_add_black_24dp" 
    android:id="@+id/nav_person_add" 
    android:title="Add people" 
    app:showAsAction="ifRoom" /> 

</menu> 
:

여기
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
setHasOptionsMenu(true); 
} 

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

가 friend_menu.xml이다 : 여기

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    // Depending on which fragment is used display different actions. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

이 책임이있는 단편의 코드이다 : 여기

이 책임이있는 활동에 코드

검색 동작과 add_friend 조치없이 원하는 메뉴를 얻으려면 어떻게해야합니까? 나는 액티비티에서 이것을 관리하는 것이 더 간단 할 수 있으며 액티비티의 주석처럼 조각이 표시 될 때만 특정 조각 메뉴를 부 풀릴 수 있습니다. // Depending on which fragment is used display different actions.

답변

1

메뉴를 유지하려는 경우 clear() 메뉴를 시도해 볼 수 있습니다. 그런 식으로 구조. 당신의 Fragment

@Override 
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
    menu.clear(); // Remove all existing items from the menu, leaving it empty as if it had just been created. 
    inflater.inflate(R.menu.friend_menu, menu); 
} 
에서