我创建了一个使用Fragment的应用程序。从我的MainActivity中我设置了ActionBar。
但在我的一个片段中,我需要修改动作图标并点击。
所以使用下面的代码,当我加载my Fragment时,它仍会显示MainActivity中的操作栏菜单
这是我的MainActivity:
public void restoreActionBar() {
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
// enable ActionBar app icon to behave as action to toggle nav drawer
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
//Handle the Search Menu
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
searchView.setQueryHint(this.getString(R.string.action_search));
((EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text))
.setHintTextColor(getResources().getColor(R.color.white));
searchView.setOnQueryTextListener(OnQuerySearchView);
mSearchCheck = false;
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}//end onCreateOptionsMenu
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
/** CAMERA **/
case R.id.action_camera:
//openCamera();
Utils.makeToast(getApplicationContext(), "Implement Camera", false);
return true;
/** SEARCH **/
case R.id.action_search:
//openSearch();
mSearchCheck = true;
Utils.makeToast(getApplicationContext(), "Implement Search", false);
return true;
/** SETTINGS **/
case R.id.action_settings:
//openSettings();
Utils.makeToast(getApplicationContext(), "Implement Settings", false);
return true;
/** ABOUT **/
case R.id.action_help:
//openHelp();
Utils.makeToast(getApplicationContext(), "Implement Help", false);
return true;
default:
return super.onOptionsItemSelected(item);
}//end switch
}//end onOptionsItemSelected
private OnQueryTextListener OnQuerySearchView = new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onQueryTextChange(String arg0) {
// TODO Auto-generated method stub
if (mSearchCheck){
// implement your search here
}
return false;
}
};//end OnQueryTextListener
这是布局:
<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="com.example.sellbeesclean.MainActivity" >
<!-- CAMERA -->
<item
android:id="@+id/action_camera"
android:orderInCategory="100"
android:icon="@drawable/ic_action_camera"
android:title="@string/action_camera"
app:showAsAction="ifRoom|collapseActionView"/>
<!-- SEARCH -->
<item
android:id="@+id/action_search"
android:orderInCategory="100"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView"/>
<!-- SETTINGS -->
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"/>
<!-- HELP -->
<item
android:id="@+id/action_help"
android:orderInCategory="100"
android:icon="@drawable/ic_action_help"
android:title="@string/action_help"
app:showAsAction="ifRoom|collapseActionView"/> </menu>
她是我的片段:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setHasOptionsMenu(true);
View rootView = inflater.inflate(R.layout.user_profile_fragment, container, false);
Log.i(TAG, "onCreateView");
.....
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.fragment_menu, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
/** EDIT **/
case R.id.action_edit:
//openEditProfile(); //Open Edit Profile Fragment
Utils.makeToast(MyApplication.getAppContext(), "Implement Camera", false);
return true;
default:
return super.onOptionsItemSelected(item);
}//end switch
}//end onOptionsItemSelected
这里是片段菜单布局
<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="com.example.sellbeesclean.MainActivity" >
<!-- EDIT -->
<item
android:id="@+id/action_edit"
android:orderInCategory="100"
android:icon="@drawable/ic_action_edit"
android:title="@string/action_edit_profile"
app:showAsAction="ifRoom|collapseActionView"/></menu>
答案 0 :(得分:33)
在片段的onCreateView方法中编写
setHasOptionsMenu(true);
并在onCreateOptionsMenu方法
中膨胀菜单xml文件在片段的onCreateOptionsMenu中,写
menu.clear();
在充气菜单之前