繁体   English   中英

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

[英]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 ,图像就会消失。

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

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

这在本机反应中怎么可能?

你能检查这段代码吗,这是一个有效的示例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
    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM