如何在 react-native 中有两个居中且可点击的图像 link

How to have two centered and clickable image link in react-native

我想让两张图片旁边的图片都可以点击,这是我的观点:

    function MyView(props) {
        return (
              <View style={{ flex: 1, }}>
                <View style={{
                  // width: 230,
                  flex: 1,
                  justifyContent: 'center',
                  alignItems: 'center',
                }}>
                  <ExternalLink href="https://play.google.com/store/apps/details">
                    <Image
                      source={availableAtGooglePlayImage}
                      style={{
                        width: '100%',
                        height: 70,
                        flex: 1,
                        marginTop: 10,
                        resizeMode: 'contain',
                      }}
                    />
                  </ExternalLink>
                </View>
                <View style={{
                  // width: 230,
                  flex: 1,
                  justifyContent: 'center',
                  alignItems: 'center',
                }}>
                  <ExternalLink href="https://itunes.apple.com/fr/app">
                    <Image
                      resizeMode="stretch"
                      source={availableAtAppStoreImage}
                      style={{
                        width: '100%',
                        height: 70,
                        flex: 1,
                        marginTop: 10,
                        resizeMode: 'contain',
                      }}
                    />
                  </ExternalLink>
                </View>
        );
    }

只要我在 ExternalLink 的父视图上使用 flex: 1,图像就会消失。

我还没有找到让这两张图片并排显示的方法。

我只找到一种方法将它们放在每个上方,并且整个宽度都是可点击的,但我只希望图像是可点击的。

这在 react-native 中如何实现?

你能检查一下这段代码吗,这是一个有效的例子expo :

import * as React from 'react';
import { Text, View, StyleSheet ,Image,TouchableOpacity,Linking } from 'react-native';




export default class App extends React.Component {
  render() {
    return (
      <View style={styles.container}>
       <View>
       <TouchableOpacity onPress={() =>Linking.openURL("https://play.google.com/store/apps/details")}>
        <Image style={{height:50,width:50}} source={{uri:"https://source.unsplash.com/random"}} />
        </TouchableOpacity>
       </View>
       <View>
        <TouchableOpacity onPress={() =>Linking.openURL("https://itunes.apple.com/fr/app")}>
        <Image style={{height:50,width:50}} source={{uri:"https://source.unsplash.com/random"}} />
        </TouchableOpacity>
       </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection:'row',
    justifyContent:'space-around',
    alignItems:'center',

  },

});

有疑问欢迎留言

您正在寻找的是 {flexDirection: 'row'} 属性 风格。 将代码复制到 https://snack.expo.io/

import * as React from 'react';
import { Text, View, StyleSheet, TouchableOpacity, Image, Linking } from 'react-native';

export default class App extends React.Component {
  render() {
    return (
      <>
        <View style={{flex:1, flexDirection:'row'}}>

          <TouchableOpacity 
            style={styles.imageButton}
            onPress={() =>{Linking.openURL("https://play.google.com/store/apps/details")}}
          >
            <Image
                resizeMode={'contain'}
                style={styles.coverImage}
                source={{uri: 'https://facebook.github.io/react/logo-og.png'}}
            />
          </TouchableOpacity>

          <TouchableOpacity 
          style={styles.imageButton}
          onPress={() =>{Linking.openURL("https://itunes.apple.com/fr/app")}}
          >
            <Image
                resizeMode={'contain'}
                style={styles.coverImage}
                source={{uri: 'https://facebook.github.io/react/logo-og.png'}}
            />
          </TouchableOpacity>
        </View>
      </>
    );
  }
}

const styles = StyleSheet.create({
    coverImage: {
        flex: 1,
        alignSelf: 'stretch',
        width: undefined,
        height: undefined
    },
    imageButton:{
      flex:1,
      height:70
    }
});