【发布时间】:2022-01-21 19:20:11
【问题描述】:
我正在尝试在 flex 行中排列 4 个 flex 项,这些项具有动态内容,因此无法为 flex 项指定宽度。
有没有办法让弹性项目动态减小它们的字体大小并占据容器的整个宽度??
我在沙箱中尝试此操作,当内容无法容纳时,下面的代码将切断第 4 行。
应用组件:
function App() {
const a = [12, 1234, 123456, 123456789];
return (
<View style={styles.app}>
{a.map((element) => {
return (
<View style={styles.item}>
<Text style={[styles.text, { fontSize: 24 }]}>{element}</Text>
</View>
);
})}
</View>
);
}
样式:
const styles = StyleSheet.create({
app: {
display: "flex",
backgroundColor: "black",
width: "100%",
height: "20%",
flexDirection: "row"
},
text: {
color: "white"
},
item: {
flexGrow: 0,
flexShrink: 0,
minWidth: "15%",
borderStartWidth: 1,
borderColor: "white",
justifyContent: "center",
padding: 5,
alignItems: "flex-start"
}
});
截图:
【问题讨论】:
-
grow and shrink 应该是 1 如果你希望它们根据可用宽度压缩和扩展
标签: css reactjs react-native flexbox