ReactNative:Styles 相互覆盖,容器错位
ReactNative:Styles overriding each other and container goes out of position
render() {
if (this.state.isLoading) {
return (
<View style={styles.container}>
<ActivityIndicator />
</View>
);
} else {
let launches = this.state.dataSource.map((item, key) => {
return (
<View key={key} style={styles.container}>
<Image
style={{ width: 350, height: 520, borderRadius: 10 }}
source={{ uri: item.rocket.imageURL }}
/>
<Entypo
name="map"
color={"white"}
size={24}
style={styles.MapIcon}
onPress={() => Linking.openURL(item.location.pads[0].mapURL)}
/>
<Entypo
name="video-camera"
color={"white"}
size={24}
style={styles.VideoIcon}
onPress={() => Linking.openURL(item.vidURLs[0])}
/>
<View style={styles.subcontainer}>
<Text style={styles.Title}>{item.missions[0].name}</Text>
<Text>
<Text style={styles.TextHeader}>Location: </Text>
<Text style={styles.Text}>{item.location.name}</Text>
</Text>
<Text>
<Text style={styles.TextHeader}>Launch Date: </Text>
<Text style={styles.Text}>{item.net}</Text>
</Text>
<Text>
<Text style={styles.TextHeader}>Service Provider: </Text>
<Text style={styles.Text}>{item.lsp.name}</Text>
</Text>
<Text>
<Text style={styles.TextHeader}>Rocket: </Text>
<Text style={styles.Text}>{item.rocket.name}</Text>
</Text>
</View>
</View>
);
});
return (
<View style={styles.Background}>
<ScrollView contentContainerStyle={styles.contentContainer}>
{launches}
</ScrollView>
</View>
);
}
}
}
const styles = StyleSheet.create({
Background: {
backgroundColor: "#e8e8e8"
},
contentContainer: {
padding: 3,
backgroundColor: "#e8e8e8"
},
subcontainer: {
backgroundColor: "rgb(249, 249, 249)",
position: "absolute",
borderRadius: 2,
padding: 10,
marginTop: 420,
width: "100.65%",
marginLeft: "2.94%",
shadowColor: "grey",
shadowOpacity: 0.5,
shadowRadius: 2,
shadowOffset: { width: 0, height: -1 }
},
container: {
flex: 1,
borderRadius: 10,
backgroundColor: "rgba(249, 249, 249, 0.8)",
padding: 10,
borderWidth: 0.5,
borderColor: "#CCCCCC",
marginTop: 4
},
Title: {
color: "#007AFF",
fontSize: 18,
textAlign: "center",
lineHeight: 18,
paddingBottom: 5,
fontWeight: "bold"
},
Text: {
color: "black",
fontSize: 14,
textAlign: "left"
},
TextHeader: {
color: "#007AFF",
fontSize: 14,
textAlign: "left",
fontWeight: "bold"
},
MapIcon: {
position: "absolute",
marginTop: "7%",
marginLeft: "8%"
},
VideoIcon: {
position: "absolute",
marginTop: "7%",
marginLeft: "92%"
}
});
下面的图片显示了更好的表示,但为了彻底解释它,我有一个容器,里面有一个子容器,其中包含有关发射名称、位置、日期、火箭等的信息,以及子容器-container 应该很好地适合每个 container/card (卡片用于每个火箭发射事件,它有自己的带有填充等的容器)但有时一些文本变得太长而不得不占用另一行。这迫使子容器向下移动(显示在“它不应该如何”图像中并从 container/card 延伸出去。Q: 有没有办法让每当文本需要另一行时,子容器会随着其中的文本向上移动,因此它仍然适合 container/card 并且不会延伸到它之外?或其他解决方案。
One way to solve this problem is to absolute position your
subContainer to the bottom of the Container using bottom:0
, instead
of providing marginTop:420
. This way the subContainer View will
always stick to the bottom of the Container View. Whereas providing
marginTop:420 will make the subContainer View always have marginTop of
420 which will push it down when it becomes big. You can also play
with left: 0 and right: 0 for the subContainer to occupy the full
width of its parent View, i.e the Container.
class App extends React.Component {
render() {
return (
<ScrollView style={{marginTop: 25}} >
{[1,2,3,4,5].map(() => (
<View style={styles.container}>
<View style={styles.subContainer} >
<Text>on every element in the calling array on every element in the calling array on every element in the calling array</Text>
</View>
</View>
))}
</ScrollView>
);
}
}
const styles = StyleSheet.create({
container: {
backgroundColor: 'blue',
height: 300,
margin: 10
},
subContainer: {
backgroundColor: '#eee',
position: 'absolute',
bottom: 0,
right: 0,
left: 0,
margin: 10,
padding: 5,
borderRadius: 2
},
});
查看工作示例:https://snack.expo.io/SyXDLkEDX
检查并判断是否有帮助。
render() {
if (this.state.isLoading) {
return (
<View style={styles.container}>
<ActivityIndicator />
</View>
);
} else {
let launches = this.state.dataSource.map((item, key) => {
return (
<View key={key} style={styles.container}>
<Image
style={{ width: 350, height: 520, borderRadius: 10 }}
source={{ uri: item.rocket.imageURL }}
/>
<Entypo
name="map"
color={"white"}
size={24}
style={styles.MapIcon}
onPress={() => Linking.openURL(item.location.pads[0].mapURL)}
/>
<Entypo
name="video-camera"
color={"white"}
size={24}
style={styles.VideoIcon}
onPress={() => Linking.openURL(item.vidURLs[0])}
/>
<View style={styles.subcontainer}>
<Text style={styles.Title}>{item.missions[0].name}</Text>
<Text>
<Text style={styles.TextHeader}>Location: </Text>
<Text style={styles.Text}>{item.location.name}</Text>
</Text>
<Text>
<Text style={styles.TextHeader}>Launch Date: </Text>
<Text style={styles.Text}>{item.net}</Text>
</Text>
<Text>
<Text style={styles.TextHeader}>Service Provider: </Text>
<Text style={styles.Text}>{item.lsp.name}</Text>
</Text>
<Text>
<Text style={styles.TextHeader}>Rocket: </Text>
<Text style={styles.Text}>{item.rocket.name}</Text>
</Text>
</View>
</View>
);
});
return (
<View style={styles.Background}>
<ScrollView contentContainerStyle={styles.contentContainer}>
{launches}
</ScrollView>
</View>
);
}
}
}
const styles = StyleSheet.create({
Background: {
backgroundColor: "#e8e8e8"
},
contentContainer: {
padding: 3,
backgroundColor: "#e8e8e8"
},
subcontainer: {
backgroundColor: "rgb(249, 249, 249)",
position: "absolute",
borderRadius: 2,
padding: 10,
marginTop: 420,
width: "100.65%",
marginLeft: "2.94%",
shadowColor: "grey",
shadowOpacity: 0.5,
shadowRadius: 2,
shadowOffset: { width: 0, height: -1 }
},
container: {
flex: 1,
borderRadius: 10,
backgroundColor: "rgba(249, 249, 249, 0.8)",
padding: 10,
borderWidth: 0.5,
borderColor: "#CCCCCC",
marginTop: 4
},
Title: {
color: "#007AFF",
fontSize: 18,
textAlign: "center",
lineHeight: 18,
paddingBottom: 5,
fontWeight: "bold"
},
Text: {
color: "black",
fontSize: 14,
textAlign: "left"
},
TextHeader: {
color: "#007AFF",
fontSize: 14,
textAlign: "left",
fontWeight: "bold"
},
MapIcon: {
position: "absolute",
marginTop: "7%",
marginLeft: "8%"
},
VideoIcon: {
position: "absolute",
marginTop: "7%",
marginLeft: "92%"
}
});
下面的图片显示了更好的表示,但为了彻底解释它,我有一个容器,里面有一个子容器,其中包含有关发射名称、位置、日期、火箭等的信息,以及子容器-container 应该很好地适合每个 container/card (卡片用于每个火箭发射事件,它有自己的带有填充等的容器)但有时一些文本变得太长而不得不占用另一行。这迫使子容器向下移动(显示在“它不应该如何”图像中并从 container/card 延伸出去。Q: 有没有办法让每当文本需要另一行时,子容器会随着其中的文本向上移动,因此它仍然适合 container/card 并且不会延伸到它之外?或其他解决方案。
One way to solve this problem is to absolute position your subContainer to the bottom of the Container using
bottom:0
, instead of providingmarginTop:420
. This way the subContainer View will always stick to the bottom of the Container View. Whereas providing marginTop:420 will make the subContainer View always have marginTop of 420 which will push it down when it becomes big. You can also play with left: 0 and right: 0 for the subContainer to occupy the full width of its parent View, i.e the Container.
class App extends React.Component {
render() {
return (
<ScrollView style={{marginTop: 25}} >
{[1,2,3,4,5].map(() => (
<View style={styles.container}>
<View style={styles.subContainer} >
<Text>on every element in the calling array on every element in the calling array on every element in the calling array</Text>
</View>
</View>
))}
</ScrollView>
);
}
}
const styles = StyleSheet.create({
container: {
backgroundColor: 'blue',
height: 300,
margin: 10
},
subContainer: {
backgroundColor: '#eee',
position: 'absolute',
bottom: 0,
right: 0,
left: 0,
margin: 10,
padding: 5,
borderRadius: 2
},
});
查看工作示例:https://snack.expo.io/SyXDLkEDX
检查并判断是否有帮助。