Dialog/DialogFragment 設置全屏/透明度相關屬性

Dialog

設置透明度和暗度

一 設置Dialog Style
<style name="load_dialog_style" parent="@style/Theme.AppCompat.Dialog.Alert">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:backgroundDimAmount">0.5</item>
       <!-- android:backgroundDimEnabled,設置爲true時候,整個Dialog的大背景就是半透明的黑色,如果設置爲false就是全透明
           android:backgroundDimAmount表示暗度,0.0f完全不暗,即背景是可見的 ,1.0f時候,背景全部變黑暗    -->
        </style>

關鍵屬性android:backgroundDimEnabled,設置爲true時候,整個Dialog的大背景就是半透明的黑色,如果設置爲false就是全透明

二 代碼設置

1、設置透明度(Dialog自身的透明度)

WindowManager.LayoutParams lp=dialog.getWindow().getAttributes();
lp.alpha = 1.0f;
dialog.getWindow().setAttributes(lp);

alpha在0.0f到1.0f之間。1.0完全不透明,0.0f完全透明,自身Dialog不可見。

2、設置黑暗度(Dialog 窗口背景的黑暗度)

WindowManager.LayoutParams lp= dialog.getWindow().getAttributes();
lp.dimAmount = 1.0f;
dialog.getWindow().setAttributes(lp);
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

true
0.5
dimAmount在0.0f和1.0f之間,0.0f完全不暗,即背景是可見的 ,1.0f時候,背景全部變黑暗
3.設置Dialog底背景模糊和黑暗度

WindowManager.LayoutParams.FLAG_BLUR_BEHIND(設置模糊)
WindowManager.LayoutParams.FLAG_DIM_BEHIND(設置暗淡)

4.清除Dialog底背景模糊和黑暗度

getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND | WindowManager.LayoutParams.FLAG_DIM_BEHIND)

DialogFragment

創建DialogFragment有2種方式,onCreateDialog和onCreateView

建議使用onCreateView,

onCreateDialog總是遇到一些奇葩問題

onCreateView

和Fragment的一樣,返回view

  @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater,container,savedInstanceState);
        View view = inflater.inflate(R.layout.qr_layout, null);
        binding = QrLayoutBinding.bind(view);
        return view;
    }
View佔滿屏幕

有2種方式

一添加style

在onCreate中
setStyle(STYLE_NORMAL,android.R.style.Theme_Black_NoTitleBar_Fullscreen);
或者
      <style name="Dialog.FullScreen" parent="Theme.AppCompat.Dialog">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowIsFloating">false</item>
    </style>
    
    
  setStyle(STYLE_NORMAL,R.style.Dialog_FullScreen);

此種方式是onCreateView中所加載的View全屏,不妨設置view的背景一看,

二 設置參數

去掉setStyle

在onStart中設置window參數

 Window window = requireDialog().getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.CENTER;
        lp.width =  WindowManager.LayoutParams.MATCH_PARENT;
        lp.height =  WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(lp);

也可以設置View的全屏

如果view不需要全屏呢?

view 不全屏,系統就會在view周圍顯示半透明蒙層效果,如果view沒設置背景顏色的話,view背景就是白色

如果需要去掉默認白色背景呢,那就需要設置BackgroundDrawable

在onActivityCreated中添加

   @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
   getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

setBackgroundDrawable此代碼在onCreateView,onActivityCreated,onStart中都可以添加,因爲getDialog已經在onCreateDialog中生成,onCreateDialog生命週期在onCreataView之前,onCreate之後

onCreateDialog

在onCreateDialog中創建AlertDialog

  @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = requireActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.qr_layout, null);
        binding = QrLayoutBinding.bind(view);
        builder.setView(view);
        return builder.create();
    }

此時無論在onCreate還是AlertDialog設置style是都不能使View全屏,系統就會在view周圍顯示半透明蒙層效果,這時只顯示View的warp_content大小,

如果不設置getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)),

Dialog是和屏幕邊緣有間距的,並不能達到屏幕的寬度

而且在華爲P30上,view顯示水平無法居中

可以在onStart中重新設置window的width參數解決這個問題

  Window window = requireDialog().getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.verticalMargin = 0.0f;
        lp.gravity = Gravity.CENTER;
        lp.width =  WindowManager.LayoutParams.MATCH_PARENT;
        lp.dimAmount = 0.7f;
        window.setAttributes(lp);

假如設置View全屏呢,需要設置該View的參數(如在onCreateDialog,onActivityCreated,onStart中)

binding.layoutCode.setLayoutParams(new LinearLayout.LayoutParams(ScreenManager.getRealWidth(),ScreenManager.getRealHeight()));

DialogFragment設置透明度/暗度/大小屬性

直接在DialogFragment中onStart()中設置,因爲Dialog是在onStart中show的

 @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null && getActivity() != null) {
           //設置窗口的背景
           dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
            WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
            lp.dimAmount = 0.0f;//設置窗口後面的暗度
           // lp.alpha = 0.8f;//設置dialog窗口的透明度
            //設置dialog窗口的寬
            lp.width = ScreenUtil.getScreenWidth(Objects.requireNonNull(getActivity())) *0.75;
            lp.height = LinearLayout.LayoutParams.WRAP_CONTENT;//設置dialog窗口的高
            dialog.getWindow().setAttributes(lp);
        }
    }

當然也可以這樣

   dialog.getWindow().setLayout(400,300);//設置窗口的寬高
   dialog.getWindow().setDimAmount(0.5f); //設置窗口後面的暗度

參考:
Dialog整個窗體背景設置爲透明
Android Dialog透明度和暗度

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章