Android 导航抽屉和对话框
Android Navigation Drawer & Dialog
是否可以显示 AlertDialog/DialogFragment 并保持导航抽屉打开?
我尝试了一个警告对话框和一个对话框片段,但是抽屉突然关闭了。
警报对话框:
new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Message")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// ...
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
对话框片段:
public class NewTermDialogFragment extends DialogFragment {
private EditText lectureName;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater layoutInflater = getActivity().getLayoutInflater();
View view = layoutInflater.inflate(R.layout.dialog_new_term, null);
lectureName = (EditText) view.findViewById(R.id.lectureText);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(view);
builder.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
return builder.create();
}
}
通话:
FragmentManager fragmentManager = getFragmentManager();
NewTermDialogFragment newTermDialogFragment = new NewTermDialogFragment();
newTermDialogFragment.setCancelable(false);
newTermDialogFragment.show(fragmentManager, "Dialog!");
布局:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/toolbar" />
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RootActivity"
tools:ignore="MergeRootFrame" />
</LinearLayout>
<fragment
android:id="@+id/navigation_drawer"
android:name="de.uni.kassel.studentenapp.fragments.NavigationDrawerFragment"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
尝试从 NavigationDrawerFragment 显示它。它应该按预期的方式工作。
很高兴对您有所帮助。
是否可以显示 AlertDialog/DialogFragment 并保持导航抽屉打开?
我尝试了一个警告对话框和一个对话框片段,但是抽屉突然关闭了。
警报对话框:
new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Message")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// ...
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
对话框片段:
public class NewTermDialogFragment extends DialogFragment {
private EditText lectureName;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater layoutInflater = getActivity().getLayoutInflater();
View view = layoutInflater.inflate(R.layout.dialog_new_term, null);
lectureName = (EditText) view.findViewById(R.id.lectureText);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(view);
builder.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
return builder.create();
}
}
通话:
FragmentManager fragmentManager = getFragmentManager();
NewTermDialogFragment newTermDialogFragment = new NewTermDialogFragment();
newTermDialogFragment.setCancelable(false);
newTermDialogFragment.show(fragmentManager, "Dialog!");
布局:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/toolbar" />
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RootActivity"
tools:ignore="MergeRootFrame" />
</LinearLayout>
<fragment
android:id="@+id/navigation_drawer"
android:name="de.uni.kassel.studentenapp.fragments.NavigationDrawerFragment"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
尝试从 NavigationDrawerFragment 显示它。它应该按预期的方式工作。
很高兴对您有所帮助。