DialogFragment中的选项菜单

问题描述 投票:0回答:4

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

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;
}

}

android dialogfragment optionmenu
4个回答
0
投票

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

在子片段中执行(在onAttach / onDetach方法中)getTargetFragment()。setMenuVisibility(true);


0
投票

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

AlertDialog.Builder(Context context,int theme)

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),theme)

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


0
投票

带有搜索(SearchView)操作的DialogFragment(片段)中的选项菜单:

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>

-1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.