简体   繁体   English

什么是 android 上的“SafeAreaView” - React Native

[英]What is the equivalent of 'SafeAreaView' on android - React Native

Basically, i am creating a react-native app and i wanted that my logo image didn't overlap with the notification/status bar and i didn't want to set a marginTop property manually because the size of the notification bar changes on different models of phones, and i found out that if i replaced my <View/> component with a <SafeAreaView/> component my problem would be solved.基本上,我正在创建一个 react-native 应用程序,我希望我的徽标图像不与通知/状态栏重叠,并且我不想手动设置marginTop属性,因为通知栏的大小在不同型号上会发生变化手机,我发现如果我用<SafeAreaView/>组件替换我的<View/>组件,我的问题将得到解决。

And while it worked great for IOS, it didn't worked at all for android.虽然它对 IOS 很有效,但对 android 根本不起作用。 Of course, after a quick research i found out that this is a IOS only component, and when you try to use it on Android the <SafeAreaView/> component returns a regular <View/> back.当然,经过快速研究,我发现这是一个仅限 IOS 的组件,当您尝试在 Android 上使用它时, <SafeAreaView/>组件会返回一个常规的<View/>

So, i am trying to find out if there's a component or a workaround that works on android.所以,我试图找出是否有适用于 android 的组件或解决方法。

Yes, status bar height changes from device to device, so you can either import status bar from react-native, or Constants from Expo so you can get the actual status bar height based on the device that is used.是的,状态栏高度因设备而异,因此您可以从 react-native 导入状态栏,或从 Expo 导入 Constants,以便您可以根据所使用的设备获取实际状态栏高度。

import { StyleSheet, Platform, StatusBar } from "react-native";
import Constants from 'expo-constants';
const statusBarHeight = Constants.statusBarHeight

You can use the react-native-status-bar-height.您可以使用 react-native-status-bar-height。https://www.npmjs.com/package/react-native-status-bar-heighthttps://www.npmjs.com/package/react-native-status-bar-height

It´s just install e and create a const to have this value like: const barHeight = getStatusBarHeight();它只是安装 e 并创建一个 const 来拥有这个值,例如: const barHeight = getStatusBarHeight(); and put it on the marginTop value: <View style = {{ marginTop: barHeight}}>并将其放在 marginTop 值上: <View style = {{ marginTop: barHeight}}>

or if you´re using styled-components you can put on your styles.js或者,如果您使用的是样式组件,您可以将其放在 style.js 上

使用<SafeAreaProvide>而不是导入
import { SafeAreaProvider } from 'react-native-safe-area-context';

I recommend using SafeAreaView from the react-native-safe-area-context package.我建议使用react-native-safe-area-context包中的 SafeAreaView。 It will take care of both the Android and iOS problems.它将处理 Android 和 iOS 问题。

import { SafeAreaView } from "react-native-safe-area-context";
import { View } from 'react-native';

export function YourComponent() {
  return (
    <SafeAreaView>
      <View></View>
    </SafeAreaView>
  );
)

This is not a case for Android, becuase even if You have full-screen app in Android, there is no possibility to overlap with status bar.这不是 Android 的情况,因为即使您在 Android 中有全屏应用程序,也不可能与状态栏重叠。 The status bar is neither always visible, or can be "dragged down" to be visible, but in both cases, the status bar is out of the working area of the application.状态栏既不是始终可见,也不是可以“向下拖动”可见,但在这两种情况下,状态栏都在应用程序的工作区域之外。

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

相关问题 React Native 中的 viewDidAppear 等价物是什么 - What is the equivalent of viewDidAppear in React Native SafeAreaView 顶部未在 react-native 中显示背景颜色 - SafeAreaView top not showing background color in react-native 使用 SafeAreaView 时在 iOS 上反应原生滚动视图抽动 - React native scroll view twitching on iOS when using SafeAreaView 在react-native中是否存在与RCTJavaScriptDidLoadNotification事件相当的Android? - Is there an Android equivalent of the RCTJavaScriptDidLoadNotification event in react-native? React - SafeAreaView 不适用于 iPhone - React - SafeAreaView not working for iPhone 什么相当于React JS的React Native {flex:1}? - What's the equivalent of React Native { flex: 1 } for React JS? React Native SafeAreaView 背景颜色 - 如何为屏幕的顶部和底部分配两种不同的背景颜色? - React Native SafeAreaView background color - How to assign two different background color for top and bottom of the screen? 如何创建一个 react-native 全屏模式,它将覆盖带有选项卡的 SafeAreaView? - How do I create a react-native full-screen modal that will cover a SafeAreaView with tabs? React Native相当于mousedown - React Native equivalent to mousedown React Native中ReactDOM和document.body的等效变量是什么? (世博会) - What are the equivalent variables for ReactDOM and document.body in React Native? (Expo)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM