android - 如何为 React Native App 提供 Android 缺口支持
问题描述
我想让我的应用程序的窗口在缺口的两侧绘制。我遵循了这篇文章的指南,并在我的res文件夹中创建了values-v28文件夹。之后,我在 values-v28 文件夹中创建了 styles.xml 并添加了以下代码
<style name="ActivityTheme">
<item name="android:windowLayoutInDisplayCutoutMode">
shortEdges
</item>
</style>
但是我的应用程序的窗口仍然总是被绘制在缺口之下。我完全困惑什么是正确的方法来做到这一点。
解决方案
我设法让它工作!
我写了这篇文章来解释这些步骤。
这是一个 TL/DR:
- 将这些行添加到您的
MainActivity.java
:
public class MainActivity extends ReactActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
+ WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
+ layoutParams.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
+ getWindow().setAttributes(layoutParams);
+ getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
+ getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
+ }
super.onCreate(savedInstanceState);
}
- 然后安装并链接react-native-safe-area-context以处理缺口填充。这是最终结果:
代码:
import React from 'react'
import { StatusBar, Text, View } from 'react-native'
import { useSafeArea } from 'react-native-safe-area-context'
export function App() {
const safeAreaInsets = useSafeArea()
return (
<View
style={{
flex: 1,
backgroundColor: '#ffca28',
paddingTop: safeAreaInsets.top,
paddingBottom: safeAreaInsets.bottom,
paddingLeft: safeAreaInsets.left,
paddingRight: safeAreaInsets.right,
}}
>
<StatusBar backgroundColor="#7cb342" />
<Text style={{ color: 'black', textAlign: 'center' }}>Top</Text>
<View
style={{
flex: 1,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
}}
>
<Text style={{ color: 'black', textAlign: 'center' }}>Left</Text>
<Text style={{ color: 'black', textAlign: 'center' }}>Right</Text>
</View>
<Text style={{ color: 'black', textAlign: 'center' }}>Bottom</Text>
</View>
)
}
推荐阅读
- uno-platform - 应用程序加载缓慢,因为 dotnet.wasm 没有为 AOT 和 InterpreterAOT 构建缓存
- angular - 输入“行为主题”
' 不可分配给类型 'BehaviorSubject ' - python - 从一个while循环跳回另一个while循环
- excel - Tcom excel - 创建一个系列中的 xy 值的图形
- python-3.x - 使用 pywinRM 远程运行 Python (PyTest)
- html - Bootstrap 5:第二列中的表跳到第一列下方
- python - 提到不支持在 python 中读取 CSV 文件
- pine-script - 绘制与 SMA 线相切的线段
- r - 从 R 中的 CSV 文件创建镶木地板文件目录
- node.js - .npmrc 忽略了私有包