DialogFragment中的选项菜单

时间:2016-07-14 09:31:31

标签: android dialogfragment optionmenu

我有两个片段,一个是列表片段,另一个是我用它作为对话框一次和片段我的问题在这里用选项菜单,在列表片段中给出的项目菜单显示总是动作在对话框片段中,我不希望这个项目可见,我也不希望选项菜单可见,我试过

setHasOptionMenu(false)

但它没有工作并尝试setHasOptionMenu(true)并清除菜单中的所有项目,但它既没有效果。

此外,我的父活动没有任何选项菜单代码。 这是我的代码:

@SuppressLint("InflateParams")
public class FormGeneratorActivity extends DialogFragment {
Control mControl;
boolean doChangeTitle;
private boolean isDialog;
public static int CreatedNum = 0;


public static FormGeneratorActivity getInstance(Control mControl, boolean doChangeTitle) {
    FormGeneratorActivity frag = new FormGeneratorActivity();
    Bundle b = new Bundle();
    b.putParcelable("control", mControl);
    b.putBoolean("changeTitle", doChangeTitle);
    frag.setArguments(b);
    return frag;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        doChangeTitle = getArguments().getBoolean("changeTitle");
    }
    setHasOptionsMenu(false);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return super.onOptionsItemSelected(item);
}

@Override
public void onPrepareOptionsMenu(Menu menu) {
    menu.clear();
    activity.invalidateOptionsMenu();
    super.onPrepareOptionsMenu(menu);

}

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



public void setDoChangeTitle(boolean doChangeTitle) {
    this.doChangeTitle = doChangeTitle;
}

View view;
boolean isInflated = false;
ParentActivity activity;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (view == null) {
        mControl = getArguments().getParcelable("control");
        view = inflater.inflate(R.layout.form_generator_fragment, container, false);
        container = (LinearLayout) view.findViewById(R.id.cont);
        isInflated = true;
    } else {
        if (view.getParent() != null)
            ((ViewGroup) view.getParent()).removeAllViews();
        isInflated = false;
    }

    return view;
}

String headerPageTitle = null;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    activity = (ParentActivity) getActivity();
    activity.getSupportActionBar().invalidateOptionsMenu();

        CreatedNum++;

}

@Override
public void onResume() {
    super.onResume();
    activity.setPageTitle(headerPageTitle);
    setHasOptionsMenu(false);
    activity.getSupportActionBar().invalidateOptionsMenu();

}


public void isDialog(boolean b) {
    isDialog = b;
}

}

4 个答案:

答案 0 :(得分:0)

您可以尝试设置父片段,如:fragment.setTargetFragment(getSupportFragmentManager()。findFragmentById(R.id.frame),1);

并在您执行的子片段中(在onAttach / onDetach方法中)     。getTargetFragment()setMenuVisibility(真);

答案 1 :(得分:0)

当您将此片段称为对话框时,可能尝试使用其中一个对话框主题。 为此,您可以使用此构造函数:

  

AlertDialog.Builder(Context context,int theme)

  

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),   主题)

更多解释: Theme not applying to DialogFragment on Android

答案 2 :(得分:0)

DialogFragment(Fragment)中具有Search(SearchView)操作的选项菜单:

MenuDialogFragment.class

public class MenuDialogFragment extends DialogFragment {

    Toolbar toolbar;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.custom_dialog_layout, container, false);

        toolbar = view.findViewById(R.id.custom_dialog_layout_toolbar);

        toolbar.inflateMenu(R.menu.menu_custom_dialog);

        Menu menu = toolbar.getMenu();
        SearchView searchView = (SearchView) menu.findItem(R.id.menu_searchview).getActionView();

        // DO whatever you need with menu items

        return view;
    }
}

menu_custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >
    <item
        android:id="@+id/menu_searchview"
        android:icon="@drawable/ic_search"
        android:title="Search"
        app:showAsAction="always|collapseActionView"
        app:actionViewClass="android.support.v7.widget.SearchView"
        />
</menu>

custom_dialog_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<android.support.v7.widget.Toolbar
    android:id="@+id/custom_dialog_layout_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    />
</LinearLayout>

答案 3 :(得分:-1)

我发现对话框片段只需要时间知道选项菜单不再存在所以我不得不在启动片段之前使用处理程序发布延迟