Android中的全屏DialogFragment(通过ActionBar)

Jos*_*tor 6 android fullscreen android-fragments android-dialogfragment

我在全屏显示DialogFragment时遇到了一些问题.在我的应用程序中,我有一个片段,当你点击某个按钮时它会调用DialogFragment.但DialogFragment只填充片段区域(在操作栏下方).我希望它像这样填满所有屏幕:http://img845.imageshack.us/img845/8682/myimage2.png

有帮助吗?

     public class ItensActivity extends Fragment {
             ... 
            public void openDialogItens()
            {       
                dialog = new ItemDialog();      
                dialog.show(getFragmentManager(), "");
                getFragmentManager().executePendingTransactions();  

            }

            public static class ItemDialog extends DialogFragment
                {


                   @SuppressWarnings("deprecation")
                   @Override
                   public Dialog onCreateDialog(Bundle savedInstanceState) {
                      AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());        
                      builder.setView(getContentView());
                      Dialog dialog = builder.create();
                      dialog.setCanceledOnTouchOutside(true);
                      WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
                      lp.copyFrom(dialog.getWindow().getAttributes());
                      lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
                      lp.height = WindowManager.LayoutParams.FILL_PARENT;
                      dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
                      dialog.getWindow().setAttributes(lp);   
                      return dialog;
                  }

            private View getContentView() {
                   LayoutInflater inflater = getActivity().getLayoutInflater();
                   View view = inflater.inflate(R.layout.popup_item, null);
                   return view;
            }
            }    
Run Code Online (Sandbox Code Playgroud)

Jos*_*tor 3

因此,经过大量搜索后,我没有找到使用 API 5 使 DialogFragment 覆盖操作栏的方法。但我找到了另一种方法来使 Fragment 在屏幕上调用以对话框为主题的新活动。这个解决方案对我帮助很大,所以,我在这里分享它: https: //stackoverflow.com/a/12275009/2327056

@StrikeForceZero 使用谷歌小组帖子中的想法,我能够将其设计为活动。您最好将高度和宽度修改为您选择的“动态”尺寸。然后设置您想要的任何 ActionBar 按钮

<style name="PopupTheme" parent="android:Theme.Holo.Light.Dialog">
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowSoftInputMode">stateAlwaysHidden</item>
    <item name="android:windowActionModeOverlay">true</item>
    <item name="android:windowIsTranslucent">true</item>
</style>
Run Code Online (Sandbox Code Playgroud)

--

public static void showAsPopup(Activity activity) {
    //To show activity as dialog and dim the background, you need to declare android:theme="@style/PopupTheme" on for the chosen activity on the manifest
    activity.requestWindowFeature(Window.FEATURE_ACTION_BAR);
    activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND,
            WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    LayoutParams params = activity.getWindow().getAttributes(); 
    params.height = 850; //fixed height
    params.width = 850; //fixed width
    params.alpha = 1.0f;
    params.dimAmount = 0.5f;
    activity.getWindow().setAttributes((android.view.WindowManager.LayoutParams) params); 
    setContentView(R.layout.activity_main);
}
Run Code Online (Sandbox Code Playgroud)