英文:
How can I position two buttons on top of the background side by side and at the bottom of the screen in React Native?
问题
在容器中,有一个position
属性设置为relative
,以及一个内部的</View>
元素,其position
属性设置为absolute
,以便按钮位于背景中的视频屏幕之上。
当我给后者设置position
属性为absolute
时,它不再位于屏幕底部,但我需要这样设置它以保持在图像之上。
以下是内部元素设置为absolute
后之前和之后的屏幕截图:
这是包括absolute
属性的代码:
if (props.stream) {
return (
<>
<TouchableWithoutFeedback onPress={handleScreenPress}>
<View style={styles.videoViewWrapper}>
<RTCView style={styles.android} streamURL={props.stream.toURL()} />
<CountDown time={time} />
{/* {showButtons && */}
<View style={{
// position: 'absolute',
}}>
<Button
icon="phone"
mode="contained"
style={{ width: 200, margin: 10 }}
onPress={handleEndCall}
>
End Call
</Button>
<Button
icon="phone"
mode="contained"
style={{ width: 200, margin: 10 }}
onPress={handleSpeakerSwitch}
>
Speaker
</Button>
</View>
{/* } */}
</View>
</TouchableWithoutFeedback>
</>
);
}
return (<><Loader title="Connecting Your Call.." /></>);
}
const styles = StyleSheet.create({
videoViewWrapper: {
flex: 1,
overflow: 'hidden'
},
android: {
flex: 1,
position: 'relative',
backgroundColor: 'black'
}
})
export default VideoCall;
我正在寻求一些提示,以便我如何将这两个按钮在底部并在图像之上排列在一起,因为我遇到了困难。
英文:
I have a container with a position of relative
and an inner </View>
element with a position of absolute
so the buttons sit on top of the video screen in the background.
When I give the latter its property of absolute
, it no longer stays at the bottom of the screen, but I need to give it this so it stays 'on top' of the image behind it.
Here are screenshots of before and after the inner element position on absolute
:
Here's my code including the absolute
property:
if (props.stream) {
return (
<>
<TouchableWithoutFeedback onPress={handleScreenPress}>
<View style={styles.videoViewWrapper}>
<RTCView style={styles.android} streamURL={props.stream.toURL()} />
<CountDown time={time} />
{/* {showButtons && */}
<View style={{
// position: 'absolute',
}}>
<Button
icon="phone"
mode="contained"
style={{ width: 200, margin: 10 }}
onPress={handleEndCall}
>
End Call
</Button>
<Button
icon="phone"
mode="contained"
style={{ width: 200, margin: 10 }}
onPress={handleSpeakerSwitch}
>
Speaker
</Button>
</View>
{/* } */}
</View>
</TouchableWithoutFeedback>
</>
);
}
return (<> <Loader title="Connecting Your Call.." /> </>)
}
const styles = StyleSheet.create({
videoViewWrapper: {
flex: 1,
overflow: 'hidden'
},
android: {
flex: 1,
position: 'relative',
backgroundColor: 'black'
}
})
export default VideoCall;
I am seeking some tips as to how I could align the two buttons side by side at the bottom and on top of the image behind it, as I'm struggling.
答案1
得分: 2
Here's the translated code:
如果您想实现类似这样的效果,请查看以下代码:
[![在这里输入图片描述][1]][1]
检查代码:
export default class App extends React.Component {
render() {
return (
<View style={{flex:1}}>
<View style={{flex:1, flexDirection:'row', alignItems:'flex-end',}}>
<TouchableOpacity
style={{width:'40%' , backgroundColor:'blue' ,marginHorizontal:10}}
>
<Text style={{color:'white',alignSelf:'center', padding:5}}>结束通话</Text>
</TouchableOpacity>
<TouchableOpacity
style={{width:'40%', backgroundColor:'blue',marginHorizontal:10}}
>
<Text style={{color:'white',alignSelf:'center', padding:5}}>接听通话</Text>
</TouchableOpacity>
</View>
</View>
);
}
}
检查链接: [expo snack][2]
希望对您有所帮助,有疑问请随意提问。
[1]: https://i.stack.imgur.com/fnMmP.png
[2]: https://snack.expo.io/B1RYno6JL
Please note that the translation is provided as per your request and only includes the code portion. If you have any doubts, feel free to ask.
英文:
If you want to achieve something like this, then see the code :
Check code :
export default class App extends React.Component {
render() {
return (
<View style={{flex:1}}>
<View style={{flex:1, flexDirection:'row', alignItems:'flex-end',}}>
<TouchableOpacity
style={{width:'40%' , backgroundColor:'blue' ,marginHorizontal:10}}
>
<Text style={{color:'white',alignSelf:'center', padding:5}}>End Call</Text>
</TouchableOpacity>
<TouchableOpacity
style={{width:'40%', backgroundColor:'blue',marginHorizontal:10}}
>
<Text style={{color:'white',alignSelf:'center', padding:5}}>Pick Call</Text>
</TouchableOpacity>
</View>
</View>
);
}
}
check the link : expo snack
Hope it helps .feel free for doubts
答案2
得分: 0
要么你可以为它们分别设置位置,第一个按钮设置为上和左,第二个按钮设置为上和右,要么将它们放在一个视图中,并将视图的flexDirection属性设置为row,这样它们可以并排放置。我有一个卡片对象,上面有两个按钮,它就是这样工作的。
英文:
Either you can give them separate positioning, top and left for the first, and top and right for the second button, or put them in a view and set flexDirection of the view to row so they could stay side by side. I have a card object and two buttons on top of it, and it works that way.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论