【问题标题】:How to set text and an icon above the image with separate view in react-native如何在 react-native 中使用单独的视图在图像上方设置文本和图标
【发布时间】:2019-08-10 06:32:16
【问题描述】:

我需要在图片上方显示文字和图标。但是这里的图像视图和文本和加载图标不需要链接。两者都需要有一个单独的视图。

我设置了两个视图(一个视图用于图像,另一个用于并在我的视图中添加 marginTop:-300 值。这是一种错误的行为。

    <View style={{ aspectRatio: 4 / 5, alignSelf: 'center', width: '100%', paddingLeft: '5%', paddingRight: '5%', opacity: 0.4,}}> 
      <FastImage resizeMode={FastImage.resizeMode.cover} style={{width: '100%',height: '100%',marginTop:18}} source={["imageUrl"]}/> 
    </View>

    <View style={{flex:1, alignSelf: 'center', alignItems: 'center', justifyContent: 'center',  marginTop:-50}}> 
      <ActivityIndicator size={"large"} color={"#ffffff"} style ={{marginTop:-700,}}/> 
      <Text style={{ fontSize: 15, fontFamily: "OpenSans-Semibold",marginTop:-300,color:'white'}}>Image captured</Text>
    </View> 

https://i.stack.imgur.com/MF5kM.png

【问题讨论】:

  • 你尝试了什么?
  • 我在上面添加了我的代码 sn-p。请重新加载并检查。我需要在图像上方显示不透明度。所以我用了这个。

标签: react-native react-native-image


【解决方案1】:

你可以使用Position

示例 This is an example 我创建的图片链接。

import React, { Component } from 'react';
import { View, Image,ActivityIndicator, Text } from 'react-native';

export default class App extends Component {
  render() {
    return (
      <View style={{flex:1,justifyContent:"center",alignItems:"center"}}>
      <View style={{opacity:0.5}}>
        <Image
          style={{width: 50, height: 50 }}
          source={require('@expo/snack-static/react-native-logo.png')}
        />
        <Image
          style={{width: 50, height: 50, }}
          source={{uri: 'https://facebook.github.io/react-native/img/tiny_logo.png'}}
        />
        <Image
          style={{width: 66, height: 58}}
          source={{uri: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADMAAAAzCAYAAAA6oTAqAAAAEXRFWHRTb2Z0d2FyZQBwbmdjcnVzaEB1SfMAAABQSURBVGje7dSxCQBACARB+2/ab8BEeQNhFi6WSYzYLYudDQYGBgYGBgYGBgYGBgYGBgZmcvDqYGBgmhivGQYGBgYGBgYGBgYGBgYGBgbmQw+P/eMrC5UTVAAAAABJRU5ErkJggg=='}}
        />
        </View>
          <ActivityIndicator size={"large"} color={"red"} style={{position:"absolute"}}/> 
         <Text style={{position:"absolute",color:"red",bottom:"45%"}} > Loading </Text>
      </View>

    );
  }
}

【讨论】:

  • 这里需要添加文字,需要在图片上方显示灰色。我已经添加了不透明度来模糊,需要添加上面没有不透明度的文本。参考我的例子。这里需要在图片上方显示文字snack.expo.io/@niph/position-exmaple
  • @sejn 如果需要不透明的值,请用视图覆盖它们。看看我更新的答案代码。
  • 它工作正常。但是加载文本和图标在同一行。所以它会重叠。我需要在图标下方显示文本。感谢您的回复
  • 谢谢,它对我有用。我已经按照你的意愿接受了。我有另一个问题。是否可以选择更改图像不透明度颜色或任何其他方式将图像叠加层更改为灰色?
  • @sejn 你可以使用rgba 示例:backgroundColor:'rgba(255,0,0,0.5)'
猜你喜欢
  • 2017-10-16
  • 2016-10-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-07
  • 1970-01-01
  • 1970-01-01
  • 2011-10-01
  • 2022-11-13
相关资源
最近更新 更多