[英]React Native - vertically centered text and button at the bottom
需要有一個布局,在其中我的居中有一個垂直對齊的文本,在右下角有一個按鈕:
注意:文本應在整個視口內垂直對齊-從頂部到bttom-例如,按鈕具有絕對位置並且不在流程中(為避免使用絕對位置,請避免使用該元素) https://imgur.com/a / YenI9oA
嘗試了flexDirection: "column"
和alignItems: "center"
用於容器, marginTop: "auto"
用於按鈕,但這僅將按鈕按在底部
另一個方向的一些實驗: https : //snack.expo.io/Bybw8xsXS
<View style={styles.container}>
<Text style={styles.text}>
Change code in the editor and watch it change on your phone! Save to get
a shareable url.
</Text>
<View style={styles.buttonContainer}>
<View style={styles.button} />
</View>
</View>
container: {
marginTop: 80,
height: 250,
borderColor: 'red',
borderWidth: 1,
// flex: 1,
flexDirection: 'row',
// alignItems: 'center',
alignContent: 'center',
flexWrap: 'wrap',
},
text: {
fontSize: 18,
borderWidth: 1,
borderColor: 'green',
},
buttonContainer: {
width: '100%',
borderWidth: 1,
borderColor: 'purple',
alignSelf: 'flex-end',
},
button: {
borderRadius: 10,
width: 50,
height: 25,
backgroundColor: 'pink',
alignSelf: 'flex-end',
}
最好使用flex。 實際上,Flexbox旨在在不同的屏幕尺寸上提供一致的布局。 有關更多信息: https : //facebook.github.io/react-native/docs/flexbox
只需使用put這樣的代碼即可:
<View style={styles.mainContainer}>
<View style={styles.container}>
<Text style={styles.text}>
Change code in the editor and watch it change on your phone! Save to get a shareable url.
</Text>
<View style={styles.buttonContainer}>
<View style={styles.button} />
</View>
</View>
</View>
在您的樣式中使用:
mainContainer: {
flex: 1,
borderColor: 'red',
borderWidth: 1,
alignItems: 'center',
justifyContent: 'center',
},
container: {
flex: 6,//you can increase it to increase the space
//borderColor: 'red',
//borderWidth: 1,
alignItems: 'center',
justifyContent: 'center',
},
text: {
fontSize: 18,
borderWidth: 1,
borderColor: 'green',
},
buttonContainer: {
flex:1
width: '100%',
//borderWidth: 1,
//borderColor: 'purple',
justifyContent: 'flex-end',
alignItems: 'flex-end',
},
button: {
borderRadius: 10,
width: 50,
height: 25,
backgroundColor: 'pink',
alignSelf: 'flex-end',
}
希望我能幫上忙。
非常簡單:只需將文本嵌套在另一個視圖中,如下所示:
const App = () => {
return (
<View style={styles.container}>
<View style={styles.textView}>
<Text style={styles.text}>
Change code in the editor and watch it change on your phone! Save to get
a shareable url.
</Text>
</View>
<View
style={{
width: '100%',
borderWidth: 1,
borderColor: 'purple',
alignSelf: 'flex-end',
}}>
<View style={styles.button} />
</View>
</View>
);
};
然后更新樣式:
container: {
marginTop: 80,
height: 250,
borderColor: 'red',
borderWidth: 1,
flex: 1,
flexWrap: 'wrap',
},
textView:{
flex: 1,
width: '100%',
justifyContent: 'center',
borderColor: 'orange',
borderWidth:1,
},
如果要復制模型,則應使用flexbox並將文本組件和按鈕組件包裝在2個單獨的容器中:
您的文本容器將具有flex 1,而按鈕容器將沒有flex。 因此,文本組件將占用所有可用空間減去按鈕組件所占用的空間。
更新資料
為了使文本在主容器中垂直居中,我看到的唯一解決方案是使按鈕容器“絕對”
<View style={styles.mainContainer}>
<View style={styles.textContainer}>
<Text style={styles.text}>
Change code in the editor and watch it change on your phone! Save to
get a shareable url.
</Text>
</View>
<View style={styles.buttonContainer}>
<View style={styles.button} />
</View>
</View>
mainContainer: {
marginTop: 200,
width: "100%",
alignItems: "center",
justifyContent: "center",
borderColor: "red",
borderWidth: 1,
height: 300
},
textContainer: {
flex: 1,
width: "100%",
alignItems: "center",
justifyContent: "center",
borderWidth: 1,
borderColor: "blue"
},
text: {
fontSize: 18,
borderWidth: 1,
borderColor: "purple"
},
buttonContainer: {
position: "absolute",
bottom: 0,
width: "100%",
justifyContent: "flex-end",
alignItems: "flex-end",
borderWidth: 1,
borderColor: "green"
},
button: {
borderRadius: 10,
width: 50,
height: 25,
backgroundColor: "blue",
alignItems: "flex-end"
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.