react native modal實(shí)現(xiàn)全屏

react native 原生modal默認(rèn)不能覆蓋Android的statusbar.這對(duì)于一個(gè)強(qiáng)迫癥患者來說真是難受。也開始嘗試將statabar透明但還是不能繪制到statusbar。最后無奈之下只好參考react native的modal去自己寫一個(gè)啦。
參考modal目錄:node_modules/react-native/ReactAndroid/src/main/java/com/facebook/react/views/modal/

3DCF58A7-5C63-45B3-927B-F60E890DA8FF.png

打開ReactModalHostManager 可以看到
public class ReactModalHostManager extends ViewGroupManager<ReactModalHostView>
ReactModalHostManager封裝的是ReactModalHostView這個(gè)ViewGroup.
于是打開ReactModalHostView這個(gè)View找到關(guān)鍵代碼

  protected void showOrUpdate() {
    // If the existing Dialog is currently up, we may need to redraw it or we may be able to update
    // the property without having to recreate the dialog
    if (mDialog != null) {
      if (mPropertyRequiresNewDialog) {
        dismiss();
      } else {
        updateProperties();
        return;
      }
    }

    // Reset the flag since we are going to create a new dialog
    mPropertyRequiresNewDialog = false;
    int theme = R.style.Theme_FullScreenDialog;
    if (mAnimationType.equals("fade")) {
      theme = R.style.Theme_FullScreenDialogAnimatedFade;
    } else if (mAnimationType.equals("slide")) {
      theme = R.style.Theme_FullScreenDialogAnimatedSlide;
    }
    Activity currentActivity = getCurrentActivity();
    Context context = currentActivity == null ? getContext() : currentActivity;
    mDialog = new Dialog(context, theme);
    mDialog.setContentView(getContentView());
    updateProperties();

    mDialog.setOnShowListener(mOnShowListener);
    mDialog.setOnKeyListener(
      new DialogInterface.OnKeyListener() {
        @Override
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
          if (event.getAction() == KeyEvent.ACTION_UP) {
            // We need to stop the BACK button from closing the dialog by default so we capture that
            // event and instead inform JS so that it can make the decision as to whether or not to
            // allow the back button to close the dialog.  If it chooses to, it can just set visible
            // to false on the Modal and the Modal will go away
            if (keyCode == KeyEvent.KEYCODE_BACK) {
              Assertions.assertNotNull(
                mOnRequestCloseListener,
                "setOnRequestCloseListener must be called by the manager");
              mOnRequestCloseListener.onRequestClose(dialog);
              return true;
            } else {
              // We redirect the rest of the key events to the current activity, since the activity
              // expects to receive those events and react to them, ie. in the case of the dev menu
              Activity currentActivity = getCurrentActivity();
              if (currentActivity != null) {
                return currentActivity.onKeyUp(keyCode, event);
              }
            }
          }
          return false;
        }
      });

    mDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    if (mHardwareAccelerated) {
      mDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
    }

    if (currentActivity == null || !currentActivity.isFinishing()) {
      mDialog.show();
    }
  }

可以看到modal其內(nèi)部也封裝的Dialog,那么我們只需要將dialog替換為popwindow是不是也可以做到呢?
于是開始寫代碼。經(jīng)過一番bug調(diào)試完成。效果如下:

modalAndroid.gif
。
注意:
1:popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//不設(shè)置 不是全屏 周圍會(huì)有空白部分
popupWindow一定要設(shè)置背景否則周圍會(huì)顯示一點(diǎn)空隙。

完整代碼:https://github.com/wuyunqiang/AndroidToRN

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容