简体   繁体   English

新手机中的 React Native 应用程序“全屏”

[英]React Native app "full screen" in new cellphones

My goal is to make an app full screen with the status bar visible in Android and in IPhone, like in Expo but in React Native我的目标是让应用程序全屏显示状态栏,在 Android 和 iPhone 中,就像在 Expo 但在 React Native 中一样

Here is what is happening:这是正在发生的事情:

My goal is to remove this gray part and make the app occupy this part and at the same time show the status bar, like in Expo.我的目标是删除这个灰色部分,让应用程序占据这部分,同时显示状态栏,就像在 Expo 中一样。

I already tried this: https://reactnative.dev/docs/statusbar.html#sethidden我已经试过这个: https://reactnative.dev/docs/statusbar.html#sethidden

But all the gray top became black and without status bar但是所有的灰色顶部都变成了黑色并且没有状态栏

I already tried add this to styles.xml in Android:我已经尝试将此添加到 Android 中的 styles.xml 中:

<item name="android:windowFullscreen">true</item>

But changed nothing (and will work just in Android)但什么都没改变(并且只适用于 Android)

Out of the box option.开箱即用的选项。

import React, { Component } from 'react';
import { StatusBar } from 'react-native';

class MyComponent extends Component {

    componentDidMount() {
       StatusBar.setHidden(true);
    }
}

or another way is to use true Immersive mode in android.或者另一种方法是在 android 中使用真正的沉浸式模式 Either you can go for native approach or find any RN modules that does this.您可以使用 go 进行本机方法,也可以找到执行此操作的任何 RN 模块。

You can use StatusBar to set those options as follows:您可以使用 StatusBar 来设置这些选项,如下所示:

import { StatusBar } from 'react-native';

export default function App() {
    React.useEffect(() => {
      StatusBar.setBackgroundColor('#FF573300'); 
      StatusBar.setTranslucent(true)
     }, []);

     return(...);
}

The last zeros in the color code indicates the opacity颜色代码中的最后一个零表示不透明度

If it only android.如果它只有 android。

1- Create FullScreenModule.java and FullScreenPackage.java and put them under android/app/main/java/com/Your-App-Name 1- 创建FullScreenModule.java and FullScreenPackage.java并将它们放在android/app/main/java/com/Your-App-Name

// FullScreenModule.java

package com.your-app-name;
import android.view.View;
import android.app.Activity;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;


public class FullScreenModule extends ReactContextBaseJavaModule {
    @Override
    public String getName() {
        return "FullScreen";
    }

    @ReactMethod
    public void enable() {
        UiThreadUtil.runOnUiThread(
            new Runnable() {
                @Override
                public void run() {
                    getCurrentActivity().getWindow().getDecorView().setSystemUiVisibility(
                        View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                        View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
                        View.SYSTEM_UI_FLAG_FULLSCREEN |
                        View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    );
                }
            }
        );

    }

    @ReactMethod
    public void disable() {
        UiThreadUtil.runOnUiThread(
            new Runnable() {
                @Override
                public void run() {
                    getCurrentActivity().getWindow().getDecorView().setSystemUiVisibility(
                        View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    );
                }
            }
        );

    }

    FullScreenModule(ReactApplicationContext reactContext) {
        super(reactContext);
    }
}

And FullScreenPackage.javaFullScreenPackage.java

// FullScreenPackage.java // FullScreenPackage.java

package com.your-app-name;

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class FullScreenPackage implements ReactPackage {

  @Override
  public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
    return Collections.emptyList();
  }

  @Override
  public List<NativeModule> createNativeModules(
                              ReactApplicationContext reactContext) {
    List<NativeModule> modules = new ArrayList<>();

    modules.add(new FullScreenModule(reactContext));

    return modules;
  }

}

Next in your MainApplication.java put those接下来在你的MainApplication.java把那些

import com.your-app-name.FullScreenPackage;

and add the package under getPackages() packages.add(new FullScreenPackage());并在getPackages() packages.add(new FullScreenPackage());

lastly create fullScrean.{js or tsx} file somewhere that containe最后在包含的地方创建fullScrean.{js or tsx}文件

import {NativeModules} from 'react-native';
module.exports = NativeModules.FullScreen;

Now import the module import FullScreen from './fullScrean';现在导入模块import FullScreen from './fullScrean';

and then simple FullScreen.enable() / FullScreen.disable() should work for you.然后简单的FullScreen.enable() / FullScreen.disable()应该适合你。

Use react-native-fullscreen-chz使用 react-native-fullscreen-chz

yarn add react-native-fullscreen-chz yarn add react-native-fullscreen-chz

import FullScreenAndroid from 'react-native-fullscreen-chz'

FullScreenAndroid.enable();

Styles.xml Styles.xml

android\app\src\main\res\values\styles.xml android\app\src\main\res\values\styles.xml

<resources xmlns:tools="http://schemas.android.com/tools">
        <style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
            <item name="android:textColor">#000000</item>
    
             <!-- Add this line for notched devices. -->
            <item name="android:windowLayoutInDisplayCutoutMode" tools:ignore="NewApi">shortEdges</item> 
        </style>
    </resources>

more info: react-native-fullscreen-chz更多信息: react-native-fullscreen-chz

Add this code in your Android Native mainActivity onCreate Method将此代码添加到您的 Android Native mainActivity onCreate 方法中

View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        decorView.setSystemUiVisibility(uiOptions);

Like this像这样

像这样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM