详细解答适用于同样陷入此类问题的人。
终于现在我能够以我想要的方式设计屏幕了,
根据我的问题,我越来越难以发表看法
另一个视图的顶部,由于一个原因,这就是我的根视图(视图 A),完全是使用边框构建的。
问题:为什么我只使用边框?
回答:其实我想用这种方式(对角相交)设计我的屏幕背景,这对我来说很容易以三角形方式构建,
这也是如何创建三角形的详细讨论,虽然有很多方法,但我选择了对我来说容易的一种。
三角形的形成我不想详细讨论,我已经在stackoverflow上回答过了,有兴趣的可以看看:
现在我假设没有人会在这件事上遇到问题,为什么我只使用边框。
回到实际问题,我在主视图/根视图(视图 A)中只有边框,这意味着视图内部没有空间来容纳其他视图作为子视图。
我尝试了我的东西,但没有工作......
人们在谈论一些花哨的词,例如 zIndex、相对位置、绝对位置。
-
zIndex: 让开发人员可以控制组件相互显示的顺序。
-
相对位置:每个React Native中每个组件的默认位置,这意味着我们的组件将遵循正常的顺序(组件写入的顺序)
-
绝对位置:您可以将任何组件的位置显式更改为绝对,该组件现在不会按照移动到顶部位置的顺序在父组件内部(或在开发人员使用Flex Direction 提到的任何方向顶部)
由于我在 View A 中没有空间,因此在没有空间(只是边框)的视图中放置一些东西甚至看起来都不是很好。
所以我在 View A 和 View B 之上创建了一个新的父级,我的问题得到了解决,因为 View A 和 View B 位于同一层,所以我们不关心 View A 里面是否有空间,我们可以简单地将两个 View 重叠...
在展示具体代码之前,我先举几个例子,根据下面的代码,所有的视图都会一个接一个地布局,并且都有默认位置(相对)
const App = () => {
const Height=Dimensions.get("screen").height
const Width=Dimensions.get("screen").width
return <View style={{height:Height,width:Width}}>
<View style={{height:300,width:300,backgroundColor:'purple',}}></View>
<View style={{height:200,width:200,backgroundColor:'blue',}}></View>
<View style={{height:100,width:100,backgroundColor:'gray',}}></View>
</View>
}
但是当我将所有视图的位置更改为 absolute 时,它们都会移动到它们的 primaryAxis 的顶部(primaryAxis 由 flexDirection 定义,它可以是行或列,默认情况下是react-native 中的列)...
const App = () => {
const Height=Dimensions.get("screen").height
const Width=Dimensions.get("screen").width
return <View style={{height:Height,width:Width}}>
<View style=
{{height:300,width:300,backgroundColor:'purple',position:'absolute'}}></View>
<View style=
{{height:200,width:200,backgroundColor:'blue',position:'absolute'}}></View>
<View style=
{{height:100,width:100,backgroundColor:'gray',position:'absolute'}}></View>
</View>
}
我这样修复我的代码。
const App = () => {
const Height=Dimensions.get("screen").height
const Width=Dimensions.get("screen").width
return <View style={{height:Height,width:Width}}>
<View style={{
flex:1,
borderTopWidth:Height/2,
borderBottomWidth:Height/2,
borderRightWidth:Width/2,
borderLeftWidth:Width/2,
borderTopColor:"gray",
borderBottomColor:"red",
borderRightColor:"red",
borderLeftColor:"gray",
}}></View>
<View style={{
position:'absolute',
height:Height,
width:Width,
backgroundColor:'transparent',
justifyContent:'center',
alignItems:'center'
}}>
<View style=
{{height:100,width:100,backgroundColor:'purple',borderRadius:20,margin:10}}>
</View>
<View style=
{{height:100,width:100,backgroundColor:'cyan',borderRadius:20,margin:10}}>
</View>
<View style=
{{height:100,width:100,backgroundColor:'pink',borderRadius:20,margin:10}}>
</View>
</View>
</View>;
}