react-native - react-native 使整个屏幕变黑
问题描述
在 react-native(expo) 中,我想在按下按钮时使整个屏幕变黑。我的意思是我想隐藏(在屏幕的所有区域(包括状态栏)顶部显示一个全黑的容器。有谁知道我该怎么做?
解决方案
是的,这很简单。
第 1 步:使用主父视图容器来包装所有内容
第 2 步:尝试根据状态为其赋予动态样式或内联样式。
第3步:按下按钮更新状态,然后一旦状态改变,视图就会覆盖整个屏幕。
第 4 步:注意 - 使用 Dimensions.get('screen').height 因为它将覆盖整个屏幕,包括状态栏。
constructor(props) {
super(props);
this.state = {
makeScreenBlack : false
};
}
render() {
const {makeScreenBlack} = this.state;
return (
<React.Fragment>
makeScreenBlack === true && <View style={styles.mainView}/>
{this.props.children} //Whatever you want to render.
<Button onPress = {() =>this.setState({makeScreenBlack : true})}/>
<React.Fragment>
);
}
export default StyleSheet.create({
mainView: {
height : Dimensions.get('screen').height,
width : Dimensions.get('screen').width,
backgroundColor : 'black'
},
})
推荐阅读
- database - 从前端用户的表单输入详细信息中获取数据库结果
- android - 启用缩小时 Google Places API 自动完成错误
- angular - 库组件中Domsanitizer的依赖注入
- python - 设计一种算法来找到两条路径之间的平均距离
- build - 在生产模式下构建会导致空白页面,而在开发模式下构建可以完美运行(Angular 8)
- java - 使用 Guava 的 EventBus 的注意事项
- java - AsyncTask 与
- java.lang.Object[] 不能转换为 java.lang.String[] - python - 如何确定 Tensorflow 模型需要多少 GPU 内存?
- java - 基于微调器选择显示 ListView
- go - Golang在超时Goroutine上的间歇性行为