簡體   English   中英

全屏DialogFragment

[英]Full screen DialogFragment

我正在嘗試創建一個寬度為MATCH_PARENT的DialogFragment,因此對話框幾乎全屏(在浮動外觀的邊緣留下填充)。 在Android中看到過這個解決方案Full Screen DialogFragment,但我試圖避免將寬度設置為1000dp的黑客攻擊。 使用我當前的布局,無論是使用FILL_PARENT還是MATCH_PARENT,它似乎都將寬度和高度設置為WRAP_CONTENT。

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

我已經將此解決方案用於Dialog(而不是DialogFragment),它按預期工作:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

這對我來說很完美。 當擴展DialogFragment覆蓋onCreateView() ......會強制所有邏輯

對話框使其全屏只是覆蓋此方法

 @Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {

    // the content
    final RelativeLayout root = new RelativeLayout(getActivity());
    root.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

    // creating the fullscreen dialog
    final Dialog dialog = new Dialog(getActivity());
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(root);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.YELLOW));
    dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

    return dialog;
}

嘗試切換到RelativeLayout而不是LinearLayout。 它適用於我的情況

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NO_TITLE,android.R.style.Theme_Holo_Light);
}

這個答案幫助我如何設置DialogFragment的寬度和高度?

int width = activity.getResources().getDisplayMetrics().widthPixels;
int height = activity.getResources().getDisplayMetrics().heightPixels;
content.setLayoutParams(new LinearLayout.LayoutParams(width, height));

我找到的最佳解決方案是將寬度或minWidth設置為某​​個任意大的值,以確保它填滿屏幕。 我不喜歡這個解決方案,但我沒有看到更好的東西。

在我的代碼中,我想要一個全屏對話框,但仍然在屏幕頂部顯示通知欄,所以我正在使用:

setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar);

這使它全屏,但我沒有使用全屏風格,所以我保留通知欄。

希望它可以幫到某人!

你也可以這樣做 -

@Override
  public void onCreate(@Nullable final Bundle savedInstanceState) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      setStyle(STYLE_NO_TITLE, android.R.style.Theme_Material_Light_NoActionBar_Fullscreen);
    } else {
      setStyle(STYLE_NO_TITLE, android.R.style.Theme_DeviceDefault_Light_NoActionBar);
    }
    super.onCreate(savedInstanceState);

  }

通過這種方式,您還可以看到版本> = lollipop的狀態欄

我已經回答了這個問題並解釋了這是最簡短有效的方法。 請檢查此主題

希望這可以幫助 :)

關於DialogFragment的工作原理。 onCreateView()添加以下內容

getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); 

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    //…
    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    //…
    return view;
}

要使用全屏對話框,您需要使用

public class CustomDialogFragment extends DialogFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        // Inflate the layout to use as dialog or embedded fragment
        return inflater.inflate(R.layout.purchase_items, container, false);
    }


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }
}

並顯示此對話框:

CustomDialogFragment newFragment = new CustomDialogFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
// To make it fullscreen, use the 'content' root view as the container
// for the fragment, which is always the root view for the activity
fragmentManager.beginTransaction().add(android.R.id.content, newFragment).commit();

注意 :您不能使用以下代碼顯示對話框,否則,對話框將顯示在屏幕中央,而不是全屏。

//don't show dialog like this
newFragment.show(fragmentManager, "dialog");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM