DialogFragment 中的选项菜单
Option menu in DialogFragment
我有两个片段,一个是列表片段,另一个是我将它用作对话框一次,作为片段我的选项菜单问题,在列表片段中有一个项目菜单给出了始终显示操作,在对话框片段中我不希望此项目可见,也不希望选项菜单也可见,我已尝试
setHasOptionMenu(false)
但它没有用,并尝试 setHasOptionMenu(true)
并清除菜单中的所有项目,但它也没有用。
此外,我的 parent activity 没有任何选项菜单代码。
这是我的代码:
@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;
}
}
您可以尝试像这样设置父片段:fragment.setTargetFragment(getSupportFragmentManager().findFragmentById(R.id.frame), 1);
并在您执行的子片段中(在 onAttach/onDetach 方法中)
getTargetFragment().setMenuVisibility(true);
当您将此片段称为对话时,也许可以尝试使用对话主题之一。
为此,您可以使用此构造函数:
AlertDialog.Builder(Context context, int theme)
即
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),
theme)
更多解释:
Theme not applying to DialogFragment on Android
我发现对话框片段只需要时间来知道选项菜单不再存在,所以我不得不post在开始片段之前延迟使用处理程序
带有搜索 (SearchView) 操作的 DialogFragment(Fragment) 中的选项菜单:
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>
我有两个片段,一个是列表片段,另一个是我将它用作对话框一次,作为片段我的选项菜单问题,在列表片段中有一个项目菜单给出了始终显示操作,在对话框片段中我不希望此项目可见,也不希望选项菜单也可见,我已尝试
setHasOptionMenu(false)
但它没有用,并尝试 setHasOptionMenu(true)
并清除菜单中的所有项目,但它也没有用。
此外,我的 parent activity 没有任何选项菜单代码。 这是我的代码:
@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;
}
}
您可以尝试像这样设置父片段:fragment.setTargetFragment(getSupportFragmentManager().findFragmentById(R.id.frame), 1);
并在您执行的子片段中(在 onAttach/onDetach 方法中) getTargetFragment().setMenuVisibility(true);
当您将此片段称为对话时,也许可以尝试使用对话主题之一。 为此,您可以使用此构造函数:
AlertDialog.Builder(Context context, int theme)
即
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), theme)
更多解释: Theme not applying to DialogFragment on Android
我发现对话框片段只需要时间来知道选项菜单不再存在,所以我不得不post在开始片段之前延迟使用处理程序
带有搜索 (SearchView) 操作的 DialogFragment(Fragment) 中的选项菜单:
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>