Android导航抽屉&对话

时间:2015-03-05 23:20:48

标签: android android-fragments navigation-drawer android-alertdialog android-dialogfragment


是否可以显示AlertDialog / DialogFragment并保持导航抽屉打开? 我尝试过警告对话框和对话框片段,但抽屉突然关闭。

AlertDialog:

    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();

DialogFragment:

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>

1 个答案:

答案 0 :(得分:0)

尝试从NavigationDrawerFragment显示它。它应该按照预期的方式工作。

很高兴它有所帮助。