2016-12-10 22 views
0

我一直在谷歌上搜索了幾個小時,並試圖無數的東西,但我不能得到這個工作...如何在不重疊狀態欄/導航欄並保持狀態欄顏色的情況下使全屏顯示DialogFragment?

這是MyFragment.java

public class MyFragment extends DialogFragment { 

    public MyFragment() { 
     // Required empty public constructor 
    } 

    public static MyFragment newInstance() { 
     MyFragment newFragment = new LoginDialogFragment(); 
     newFragment.setCancelable(false); 
     return newFragment; 
    } 

    @NonNull 
    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     return new Dialog(getActivity(), R.style.MyAppTheme_Dialog); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_my, container, false); 
     ButterKnife.bind(this, view); 
     return view; 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

     Window window = getDialog().getWindow(); 

     if (window != null) { 
      window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT); 
      window.setWindowAnimations(R.style.MyAppTheme_Dialog); 
     } 
    } 

} 

這是styles.xml

<style name="MyAppTheme" parent="PreferenceFixTheme.Light.NoActionBar"> 
    <item name="android:windowBackground">@color/background</item> 
    <item name="android:colorBackground">@color/background</item> 
    <item name="colorPrimary">@color/primary</item> 
    <item name="colorPrimaryDark">@color/primary_dark</item> 
    <item name="colorAccent">@color/accent</item> 
    <item name="preferenceCategory_marginBottom">0dp</item> 
</style> 

<style name="MyAppTheme.Dialog"> 
    <item name="android:backgroundDimEnabled">false</item> 
    <item name="android:windowBackground">@null</item> 
    <item name="android:windowContentOverlay">@null</item> 
    <item name="android:windowEnterAnimation">@anim/slide_up</item> 
    <item name="android:windowExitAnimation">@anim/slide_down</item> 
    <item name="android:windowFrame">@null</item> 
    <item name="android:windowIsFloating">true</item> 
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item> 
</style> 

這幾乎可以像我期望的那樣工作......唯一的問題是調用window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);填充整個屏幕(減去狀態/導航欄)會將狀態欄顏色更改爲黑色。當我關閉對話框時,它會變回colorPrimaryDark

如果我刪除window.setLayout(...)行,狀態欄將保留colorPrimaryDark,但對話框大小將爲默認值(屏幕中間的一個小方塊)。

在同一個地方我打電話window.setLayout(...),我試圖把下面的代碼:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); 
    getWindow().setStatusBarColor(ContextCompat.getColor(getActivity, R.color.primary_dark)); 
} 

這將設置狀態欄顏色colorPrimaryDark但兩者重疊的狀態和導航欄。

我只是不能全屏,沒有重疊的狀態和導航欄,並保持colorPrimaryDark在狀態欄上。

我發現了幾個同樣問題的問題,但我無法得到建議。無論是有一個接受的答案:

+0

是否必須是DialogFragment?活動是否有效? – Karakuri

+0

你是否嘗試過使用overrided dialogfragment。我有代碼。我不知道哪個能滿足你的需求。任何我會發布代碼的方式。嘗試一下。 – Noorul

+1

你爲什麼試圖強制一個對話框不是對話框?你似乎在使用錯誤的抽象。 – ianhanniballake

回答

1

檢查這個定製對話框片段,試試吧,將滿足您的需求。並讓我知道。

import android.content.pm.ActivityInfo; 
import android.content.res.Configuration; 
import android.graphics.Point; 
import android.os.Build; 
import android.os.Bundle; 
import android.support.v4.app.DialogFragment; 
import android.util.TypedValue; 
import android.view.Display; 

/** 
    * Created by Noorul on 02-07-16. 
*/ 

public class _DialogFragment extends DialogFragment { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    // preventScreenRotation(); 
    } 

    @Override 
    public void onDestroy() { 
    // releaseScreenRotation(); 
    super.onDestroy(); 
} 

@SuppressWarnings("deprecation") 
@Override 
public void onStart() { 
    super.onStart(); 

    // change dialog width 
    if (getDialog() != null) { 

     int fullWidth = getDialog().getWindow().getAttributes().width; 
     int fullHeight = getDialog().getWindow().getAttributes().height; 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { 
      Display display = getActivity().getWindowManager().getDefaultDisplay(); 
      Point size = new Point(); 
      display.getSize(size); 

      fullWidth = size.x; 
      fullHeight = size.y; 
     } else { 
      Display display = getActivity().getWindowManager().getDefaultDisplay(); 
      fullWidth = display.getWidth(); 
      fullHeight = display.getHeight(); 
     } 

     final int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 0, getResources() 
       .getDisplayMetrics()); 

     int w = fullWidth - padding; 
     int h = fullHeight - (padding * 2); 

     getDialog().getWindow().setLayout(w, h); 
    } 
} 


private void preventScreenRotation() { 
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { 
       getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
    } else { 
     getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
} 
    } 

    private void releaseScreenRotation() { 
     getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); 
} 
} 

更改對話框的大小取決於您的需要。

+0

這幾乎奏效了......我忘記提及一個關於我的問題的細節。我在這個片段上有一個向上滑動的動畫(從下到上),這段代碼片段的大小與我期望的大小相同,狀態欄將保持它的顏色。唯一的問題是,片段將在底部導航欄的頂部,而滑動... :( –

+0

你能分享我的截圖嗎? – Noorul

+0

我不能有一個實際的截圖有這尚未發佈,但它是非常與此類似:https://i.stack.imgur.com/cppoj.png - 看看「寫評論」位如何與導航欄按鈕重疊?這就是幻燈片動畫過程中發生的情況。像這樣(但使用正確的狀態欄顏色):https://i.stack.imgur.com/HiSv9.png - 這是我想要的。 –

0

根據材料設計指南創建全屏對話框。通過

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

調用此對話框:

getSupportFragmentManager().beginTransaction().add(android.R.id.content, FullScreenDialog.newInstance()).addToBackStack(null).commit(); 

設置上邊距在基地佈局,以24dp 將這個代碼在DialogFragment。這樣對話框不會重疊狀態欄。