javascript - 在 React Native 中调整图像大小

标签 javascript reactjs react-native

我正在尝试在我的 React native 应用程序中调整图像大小(缩小以适合屏幕),但我无法执行此操作,因为它太大了。

代码如下:

'use strict';

var React = require('react-native');
var {
  StyleSheet,
  Text,
  TextInput,
  View,
  TouchableHighlight,
  ActivityIndicatorIOS,
  Image,
  Component
} = React;

var styles = StyleSheet.create({
  description: {
    marginBottom: 20,
    fontSize: 18,
    textAlign: 'center',
    color: '#656565'
  },
  container: {
    padding: 30,
    marginTop: 65,
    alignItems: 'center'
  },
  flowRight: {
    flexDirection: 'row',
    alignItems: 'center',
    alignSelf: 'stretch'
  },
  buttonText: {
    fontSize: 18,
    color: 'white',
    alignSelf: 'center'
  },
  button: {
    height: 36,
    flex: 1,
    flexDirection: 'row',
    backgroundColor: '#48BBEC',
    borderColor: '#48BBEC',
    borderWidth: 1,
    borderRadius: 8,
    marginBottom: 10,
    alignSelf: 'stretch',
    justifyContent: 'center'
  },
  searchInput: {
    height: 36,
    padding: 4,
    marginRight: 5,
    flex: 4,
    fontSize: 18,
    borderWidth: 1,
    borderColor: '#48BBEC',
    borderRadius: 8,
    color: '#48BBEC'
  },
  image: {
    width: 200,
    height: 220
  },
});

class SearchPage extends Component {
  render() {
    return (
      <View style={styles.container}>

        <Image source={require('image!rclogo')} style={styles.image}/>

        <Text style={styles.description}>
          Search for RCers!
        </Text>

        <Text style={styles.description}>
          Search
        </Text>

        <View style={styles.flowRight}>
          <TextInput
            style={styles.searchInput}
            placeholder='Search by Batch, Name, Interest...'/>
          <TouchableHighlight style={styles.button}
              underlayColor='#99d9f4'>
            <Text style={styles.buttonText}>Go</Text>
          </TouchableHighlight>
        </View>

        <TouchableHighlight style={styles.button}
            underlayColor='#99d9f4'>
          <Text style={styles.buttonText}>Location</Text>
        </TouchableHighlight>

      </View>
    );
  }
} 

我试图将它从容器标签中取出,但似乎无法理解为什么它无法正确呈现?

这可以用 flexbox resizeMode 来完成吗?你怎么做呢?我找不到关于它的任何文档...

最佳答案

图像自动修复 View

image: {
    flex: 1,
    width: null,
    height: null,
    resizeMode: 'contain'
}

关于javascript - 在 React Native 中调整图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29476165/

相关文章:

javascript - 你如何命名/制作类似这个例子的东西(有点固定滚动)

reactjs - 为什么 Tailwind 类在我的 Vite React 项目中没有生效?

react-native - react native : e. nativeEvent.key == 'Enter' 不起作用

android - 在 react-native 中用连字符换行

javascript - 来自 Angular 1 背景时在 Angular 2 中思考

javascript - Three.js Collada 模型显示黑色,直到鼠标移动为止没有灯光

javascript - 当我按下按钮时如何获取特定数据

javascript - ReactJS:有更好的方法吗?

javascript - "single source of truth"是什么意思?

react-native - 实现排毒后 Jest 测试被破坏