Javascript 如何在react native中将文本放置在图像上?
如何在react native?中将文本垂直放置在图像上。但是我不能这样做,我不能添加Javascript 如何在react native中将文本放置在图像上?,javascript,image,react-native,jsx,Javascript,Image,React Native,Jsx,如何在react native?中将文本垂直放置在图像上。但是我不能这样做,我不能添加texttag作为Imagetag的子组件 <Card> <CardSection> <View style={styles.container}> <Image source={require('../Images/4.jpg')} style={styles.imageStyl} /> &
text
tag作为Image
tag的子组件
<Card>
<CardSection>
<View style={styles.container}>
<Image source={require('../Images/4.jpg')} style={styles.imageStyl} />
<Text style={styles.userStyle}>
{this.props.cat.name}
</Text>
</View>
</CardSection>
</Card>
const styles= StyleSheet.create({
container:{
flex: 1,
alignItems: 'stretch',
justifyContent: 'center',
},
imageStyl: {
flexGrow:1,
width:"100%",
height:200,
alignItems: 'center',
justifyContent:'center',
},
userStyle:{
fontSize:18,
color:'black',
fontWeight:'bold',
textAlign: 'center'
},
});
{this.props.cat.name}
const styles=StyleSheet.create({
容器:{
弹性:1,
对齐项目:“拉伸”,
为内容辩护:“中心”,
},
imageStyl:{
flexGrow:1,
宽度:“100%”,
身高:200,
对齐项目:“居中”,
辩护内容:'中心',
},
用户样式:{
尺码:18,
颜色:'黑色',
字体权重:'粗体',
textAlign:“中心”
},
});
如何将文本放置在图像的中心?获得类似这样的内容必须在“css”中使用
位置:'absolute'
然后使用css属性(如顶部、底部、右侧、左侧)放置文本
将要在视图中居中的子对象包裹起来,并使视图成为绝对视图
居中文本
它与普通HTML和CSS非常相似,将文本放置在图像上。您必须通过使文本成为绝对文本,将文本放置在图像上
- 按原样修改这些代码:
userStyle:{ position : absolute; bottom : 0, top : 50, left : 0, right : 0, alignItems: 'center', justifyContent:'center', }
我希望这些代码能解决你的问题 我有自己的组件来执行此操作:
import React from 'react';
import { View, Image } from 'react-native';
const BackgroundImage = (props) => {
const { container, image } = styles;
return (
<View style={container}>
<Image
style={[image,
{ resizeMode: props.resizeMode,
opacity: props.opacity}
]}
source={props.source}***strong text***
/>
</View>
)
};
const styles = {
container: {
position: 'absolute',
top: 0,
left: 0,
width: '100%',
height: '100%',
},
image: {
flex: 1,
}
};
export {BackgroundImage};
从“React”导入React;
从“react native”导入{View,Image};
const BackgroundImage=(道具)=>{
const{container,image}=样式;
返回(
)
};
常量样式={
容器:{
位置:'绝对',
排名:0,
左:0,,
宽度:“100%”,
高度:“100%”,
},
图像:{
弹性:1,
}
};
导出{BackgroundImage};
该组件将用您想要的任何图像填充您的容器;)
从“React”导入React;
从“react native”导入{View,Image};
类列表扩展组件{
render(){
让source={uri:'http://via.placeholder.com/350x150'};
返回(
你好,世界
)
}
导出默认列表;
使用以下方法:
<ImageBackground source={require('background image path')} style={{width: '100%', height: '100%'}}>
<View style={{position: 'absolute', top: 0, left: 0, right: 0, bottom: 0, justifyContent: 'center', alignItems: 'center'}}>
<Text>Centered text</Text>
</View>
</ImageBackground>
居中文本
您可以从react native
导入ImageBackground
而不是Image
,并将文本作为子对象传递到ImageBackground
。这会产生神奇的效果。请参见下面的代码:
<View style={styles.imageWrapper}>
<ImageBackground style={styles.theImage} source={{uri : item.imageUrl}}>
<Text>Hey</Text>
</ImageBackground>
</View>
const styles = StyleSheet.create({
imageWrapper: {
height: 200,
width: 200,
overflow : "hidden"
},
theImage: {
width: "100%",
height: "100%",
resizeMode: "cover",
}
})
嘿
const styles=StyleSheet.create({
图像包装器:{
身高:200,
宽度:200,
溢出:“隐藏”
},
图像:{
宽度:“100%”,
高度:“100%”,
resizeMode:“封面”,
}
})
正如许多人所说,我们也可以使用定位,但我更喜欢使用
ImageBackground
这是实现这一点的更方便的方法。问题是如何反应
<View style={styles.imageWrapper}>
<ImageBackground style={styles.theImage} source={{uri : item.imageUrl}}>
<Text>Hey</Text>
</ImageBackground>
</View>
const styles = StyleSheet.create({
imageWrapper: {
height: 200,
width: 200,
overflow : "hidden"
},
theImage: {
width: "100%",
height: "100%",
resizeMode: "cover",
}
})