android - 反应填充所有手机屏幕尺寸的本机网格,滑动时将消失
问题描述
我怎样才能像 React Native 的图像中那样创建方框网格,并且这些方框将填满手机的所有屏幕尺寸?
对于每个框,如果用户获得滑动手势,它将从屏幕上消失,但所有未触摸的框保持在相同的坐标/位置。如果所有框都消失,则程序将继续。
到目前为止,我已经尝试过这种方法来显示框:`
import React from "react";
import {
StyleSheet,
Text,
View,
Dimensions,
StatusBar,
TouchableOpacity
} from "react-native";
export default class App extends React.Component {
componentDidMount() {
StatusBar.setHidden(true);
}
createBoxes = numberOfBoxes => {
let boxes = [];
// Outer loop to create parent
for (let i = 0; i < numberOfBoxes; i++) {
boxes.push(
<TouchableOpacity
key={i}
style={styles.box}
onPress={() => {
console.log("number "+ i);
}}
>
<Text>{i}</Text>
</TouchableOpacity>
);
}
return boxes;
};
render() {
const boxHeight = 29;
const height = Math.ceil(Dimensions.get("window").height);
let totalBox = Math.round(height / boxHeight) * 9; //9 is the number of box on screen
console.log(height, boxHeight, totalBox);
return <View style={styles.container}>{this.createBoxes(totalBox)}</View>;
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: "row",
backgroundColor: "#fff",
flexWrap: "wrap",
alignItems: "flex-start"
},
box: {
width: "10%",
height: 25,
margin: 2,
backgroundColor: "powderblue"
}
});
` 但结果不适合不同的手机比例(16:9 的结果还可以,但 18:9 的手机底部有一点空间)
如何做到这一点?
解决方案
您需要通过添加以下代码来更新容器样式,如下所示:
justifyContent: "space-evenly"
请在下面找到更新的代码:
import React from "react";
import {
StyleSheet,
Text,
View,
Dimensions,
StatusBar,
TouchableOpacity
} from "react-native";
export default class App extends React.Component {
componentDidMount() {
StatusBar.setHidden(true);
}
createBoxes = numberOfBoxes => {
let boxes = [];
// Outer loop to create parent
for (let i = 0; i < numberOfBoxes; i++) {
boxes.push(
<TouchableOpacity
key={i}
style={styles.box}
onPress={() => {
console.log("number "+ i);
}}
>
<Text>{i}</Text>
</TouchableOpacity>
);
}
return boxes;
};
render() {
const boxHeight = 29;
const height = Math.ceil(Dimensions.get("window").height);
let totalBox = Math.round(height / boxHeight) * 9; //9 is the number of box on screen
console.log(height, boxHeight, totalBox);
return <View style={styles.container}>{this.createBoxes(totalBox)}</View>;
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: "row",
backgroundColor: "#fff",
flexWrap: "wrap",
alignItems: "flex-start",
justifyContent: "space-evenly"
},
box: {
width: "9%",
height: 25,
margin: '0.5%',
aspectRatio: 1,
backgroundColor: "powderblue"
}
});
推荐阅读
- java - 底部导航视图
- node.js - 在节点上但在 nodemon 上加载静态文件
- python - Tensorflow 错误 - 参数单元不是 rnn 单元,缺少属性,需要方法,不可调用
- javascript - `import * from ''` 和 `import * as "" from""` 有什么区别?
- java - 我该如何解决这个问题?java.io.IOException:流已关闭
- gitlab-ci - 为什么 gitlab runner 挂了?
- php - 在 Google App Engine 部署期间未找到“Laravel\Dusk\DuskServiceProvider”类
- flutter - 参数类型 '({growable: bool}) → List
' 不能分配给参数类型 'List ' - wordpress - woocommerce 变体通过编程/css 选择
- java - 使用 yaml 中的 micronaut 框架配置 Cassandra