1

是否可以创建DialogFragment并更改默认容器? 我试过DialogFragment.show(Transaction, ...)并在那里设置容器,但它告诉我Fragment already added。 DialogFragment的行为与正常的DialogFragment相似,这一点很重要。为DialogFragment选择容器

编辑:我觉得有一些误解。我说它应该看起来像“一个普通的DialogFragment”。我的意思是应该看起来像一个正常的AlertDialog。

+0

添加一些更多的细节,如代码,异常消息。并且是“默认容器”与自定义对话框视图有关。 – Pravin 2014-11-03 10:28:43

+0

这是一个普遍的问题。我想要在其他任何地方都有锁屏。所以我需要将我的DialogFragments放置在一个容器中,它位于lockscreencontainer的下面。 – Kuno 2014-11-03 10:47:05

+0

锁屏是小部件,当屏幕锁定时调用它,我们使用getWindow()。addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); – 2014-11-05 13:40:35

回答

1

我想因为DialogFragmentFragment延伸,你可以使用它作为一个正常Fragment在任何需要的地方。它仍应实施所有Fragment生命周期方法。但是,我不确定它是否为特殊目的而覆盖其中的任何一个。

0

我这里是从DialogFragment

public static class MyDialogFragment extends DialogFragment { 
     int mNum; 

    /** 
    * Create a new instance of MyDialogFragment, providing "num" 
    * as an argument. 
    */ 
    static MyDialogFragment newInstance(int num) { 
     MyDialogFragment f = new MyDialogFragment(); 

     // Supply num input as an argument. 
     Bundle args = new Bundle(); 
     args.putInt("num", num); 
     f.setArguments(args); 

     return f; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mNum = getArguments().getInt("num"); 

     // Pick a style based on the num. 
     int style = DialogFragment.STYLE_NORMAL, theme = 0; 
     switch ((mNum-1)%6) { 
      case 1: style = DialogFragment.STYLE_NO_TITLE; break; 
      case 2: style = DialogFragment.STYLE_NO_FRAME; break; 
      case 3: style = DialogFragment.STYLE_NO_INPUT; break; 
      case 4: style = DialogFragment.STYLE_NORMAL; break; 
      case 5: style = DialogFragment.STYLE_NORMAL; break; 
      case 6: style = DialogFragment.STYLE_NO_TITLE; break; 
      case 7: style = DialogFragment.STYLE_NO_FRAME; break; 
      case 8: style = DialogFragment.STYLE_NORMAL; break; 
     } 
     switch ((mNum-1)%6) { 
      case 4: theme = android.R.style.Theme_Holo; break; 
      case 5: theme = android.R.style.Theme_Holo_Light_Dialog; break; 
      case 6: theme = android.R.style.Theme_Holo_Light; break; 
      case 7: theme = android.R.style.Theme_Holo_Light_Panel; break; 
      case 8: theme = android.R.style.Theme_Holo_Light; break; 
     } 
     setStyle(style, theme); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     // put here any view you want 
     View v = inflater.inflate(R.layout.fragment_dialog, container, false); 
     View tv = v.findViewById(R.id.text); 
     ((TextView)tv).setText("Dialog #" + mNum + ": using style " 
       + getNameForNum(mNum)); 

     // Watch for button clicks. 
     Button button = (Button)v.findViewById(R.id.show); 
     button.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       // When button is clicked, call up to owning activity. 
       ((FragmentDialog)getActivity()).showDialog(); 
      } 
     }); 

     return v; 
    } 
} 



void showDialog() { 
    mStackLevel++; 

    // DialogFragment.show() will take care of adding the fragment 
    // in a transaction. We also want to remove any currently showing 
    // dialog, so make our own transaction and take care of that here. 
    FragmentTransaction ft = getFragmentManager().beginTransaction(); 
     // may you got error here 
    Fragment prev = getFragmentManager().findFragmentByTag("dialog"); 
    if (prev != null) { 
    // remove fragment from stack will fix the problem 
     ft.remove(prev); 
    } 
    ft.addToBackStack(null); 

    // Create and show the dialog. 
    DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel); 
    newFragment.show(ft, "dialog"); 
} 

,如果你想在报警按钮使用它这里是代码

public static class MyAlertDialogFragment extends DialogFragment { 

    public static MyAlertDialogFragment newInstance(int title) { 
     MyAlertDialogFragment frag = new MyAlertDialogFragment(); 
     Bundle args = new Bundle(); 
     args.putInt("title", title); 
     frag.setArguments(args); 
     return frag; 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     int title = getArguments().getInt("title"); 

     return new AlertDialog.Builder(getActivity()) 
       .setIcon(R.drawable.alert_dialog_icon) 
       .setTitle(title) 
       .setPositiveButton(R.string.alert_dialog_ok, 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int whichButton) { 
          ((FragmentAlertDialog)getActivity()).doPositiveClick(); 
         } 
        } 
       ) 
       .setNegativeButton(R.string.alert_dialog_cancel, 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int whichButton) { 
          ((FragmentAlertDialog)getActivity()).doNegativeClick(); 
         } 
        } 
       ) 
       .create(); 
    } 
} 
+0

alertdialog按钮怎么样?我是否必须模仿我的布局中的外观? – Kuno 2014-11-05 13:22:01

+0

不,你可以随意使用它,阅读文档,你会发现关于这个的例子,我也会为你更新答案 – 2014-11-05 13:24:51

+0

这是行不通的。如果您嵌入了DialogFragment,则不会调用onCreateDialog。所以要么你可以选择一个容器,但不能有一个本地的alertDialog,或者你可以有一个本地的AlertDialog,但是不能将它嵌入到你的布局中。它是否正确? – Kuno 2014-11-05 13:51:30

1

你可以使用dialogFragment像一个普通的片段。看看样本中的这段代码。 在此代码中,它创建一个dialogFragment并将其添加到framlayout,并在按下按钮时将其显示为对话框。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:padding="4dip" 
    android:gravity="center_horizontal" 
    android:layout_width="match_parent" android:layout_height="match_parent"> 

    <TextView 
      android:id="@+id/text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0" 
      android:layout_gravity="center_vertical|center_horizontal" 
      android:gravity="top|center_horizontal" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:text="Demonstrates the same fragment 
      being shown as a dialog and embedded inside of an activity." /> 

    <Button android:id="@+id/show_dialog" 
     android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:text="show"> 
     <requestFocus /> 
    </Button> 

    <View android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" /> 

    <TextView 
      android:id="@+id/inline_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0" 
      android:layout_gravity="center_vertical|center_horizontal" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:text="Fragment embedded inside 
     of the activity:" /> 

    <FrameLayout 
      android:id="@+id/embedded" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0" 
      android:layout_gravity="center_vertical|center_horizontal" 
      android:padding="6dp" 
      android:background="#ff303030" 
      android:gravity="top|center_horizontal" /> 

</LinearLayout> 

和java代码:

public class FragmentDialogOrActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.fragment_dialog_or_activity); 

     if (savedInstanceState == null) { 
      // First-time init; create fragment to embed in activity. 

      FragmentTransaction ft = getFragmentManager().beginTransaction(); 
      DialogFragment newFragment = MyDialogFragment.newInstance(); 
      ft.add(R.id.embedded, newFragment); 
      ft.commit(); 

     } 

     // Watch for button clicks. 
     Button button = (Button)findViewById(R.id.show_dialog); 
     button.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       showDialog(); 
      } 
     }); 
    } 


    void showDialog() { 
     // Create the fragment and show it as a dialog. 
     DialogFragment newFragment = MyDialogFragment.newInstance(); 
     newFragment.show(getFragmentManager(), "dialog"); 
    } 



    public static class MyDialogFragment extends DialogFragment { 
     static MyDialogFragment newInstance() { 
      return new MyDialogFragment(); 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      View v = inflater.inflate(R.layout.hello_world, container, false); 
      View tv = v.findViewById(R.id.text); 
      ((TextView)tv).setText("This is an instance of MyDialogFragment"); 
      return v; 
     } 
    } 

} 

,并为你的错误,你可以使用下面的代码:你注意到我改变了标签名称,以便您可以对话框区分

public static void showMyDialogFragment(FragmentManager fm){ 

    FragmentTransaction ft = fm.beginTransaction(); 
    Fragment prev = fm.findFragmentByTag("MyDialogFragment"); 
    if (prev != null) { 
     ft.remove(prev); 
    } 
    ft.addToBackStack(null); 
    DialogFragment newFragment = MyDialogFragment.newInstance(); 
    newFragment.show(ft, "MyDialogFragment"); 
} 

片段和普通片段添加到您的布局。

+0

看看我的op编辑。我的意思是它应该看起来像一个alertdialog。我想在你的例子中你只有一个textView。没有标题,没有对话框按钮。如果你说DialogFagment.show,你不能挑选容器。所以这是行不通的。 – Kuno 2014-11-05 13:48:26

+1

当我使用MyDialogFragment扩展DialogFragment时,这意味着它可以是任何你喜欢的东西。你可以添加按钮到片段布局创建自定义标题和你可以想象的一切。这只是一个例子,你可以用你喜欢的任何东西来替换'R.layout.hello_world'。 – mmlooloo 2014-11-05 13:58:53

+0

所以我必须为我的标题和按钮收集所有需要的样式,让它看起来像一个AlertDialog? – Kuno 2014-11-05 14:05:40