【问题标题】:React Native how to wrap component into TouchableOpacity within a TextReact Native如何将组件包装到Text中的TouchableOpacity
【发布时间】:2020-07-02 07:34:59
【问题描述】:

我正在构建条款和条件文本,我希望我的条款和条件具有TouchableOpacity行为,PrivacyPolicy也是如此

如下所示:

现在我想将 TouchableOpacity 行为添加到 条款和条件隐私政策 相同,但是当我包装 条款和条件隐私政策TouchableOpacity 它开始变成如下所示:

即使您使用的是flexWrap: 'wrap',条款和条件也已移至新行,并且仍有空间。

下面是我的完整代码:

const styles = StyleSheet.create({
  termNConWrapper: {
    ...marginHelper(normalize(4), 0, normalize(5), 0).margin,
    width: wp('80%'),
    flexDirection: 'row',
    flexWrap: 'wrap',
  },
  termNconAgreement: {
    ...fontHelper(
      10,
      typographyFonts.openSansRegular,
      typographyColors.description,
      0.07,
      16,
    ).font,
    // textAlign: 'center',
  },
  termNcon: fontHelper(
    10,
    typographyFonts.openSansRegular,
    colors.primary,
    0.1,
    16,
  ).font,
});

const OnboardTermsNCondition = () => (
  <View style={styles.termNConWrapper}>
    <Text style={styles.termNconAgreement}>
      By clicking Sign Up, you acknowledge that you have read and agree to the
    </Text>
    <TouchableOpacity activeOpacity={0.4}>
      <Text style={styles.termNcon}>{' Terms & Conditions '}</Text>
    </TouchableOpacity>
    <Text style={styles.termNconAgreement}>and</Text>
    <TouchableOpacity activeOpacity={0.4}>
      <Text style={styles.termNcon}> Privacy Policy</Text>
    </TouchableOpacity>
    {/* <Text style={styles.termNconAgreement}>
      By clicking Sign Up, you acknowledge that you have read and agree to the
      <Text style={styles.termNcon}>{' Terms & Conditions '}</Text>
      and
      <Text style={styles.termNcon}> Privacy Policy</Text>
    </Text> */}
  </View>
);

如果有人可以提供帮助,我们将不胜感激。谢谢

编辑:我尝试删除辅助样式,但仍然没有运气。下面是完整的代码:

import React, { memo } from 'react';
// import {  } from 'native-base';
import { StyleSheet, Text, View, TouchableOpacity } from 'react-native';
import { fontHelper, marginHelper } from 'constants/theme/helpers';
import { typographyFonts, colors, typographyColors } from 'constants/theme';
import { normalize } from 'utils/normalize';
import { widthPercentageToDP as wp } from 'react-native-responsive-screen';

const styles = StyleSheet.create({
  termNConWrapper: {
    // ...marginHelper(normalize(4), 0, normalize(5), 0).margin,
    // width: wp('80%'),
    // flexDirection: 'row',
    // flexWrap: 'wrap',
  },
  termNconAgreement: {
    // ...fontHelper(
    //   10,
    //   typographyFonts.openSansRegular,
    //   typographyColors.description,
    //   0.07,
    //   16,
    // ).font,
    // textAlign: 'center',
  },
  termNcon: {},
});

const OnboardAgreement = () => (
  <View style={styles.termNConWrapper}>
    <Text style={styles.termNconAgreement}>
      By clicking Sign Up, you acknowledge that you have read and agree to the
      <TouchableOpacity activeOpacity={0.4}>
        <Text style={styles.termNcon}>{' Terms & Conditions '}</Text>
      </TouchableOpacity>
      and
      <TouchableOpacity activeOpacity={0.4}>
        <Text style={styles.termNcon}> Privacy Policy</Text>
      </TouchableOpacity>
    </Text>
  </View>
);

const MemoizedOnboardAgreement = memo(OnboardAgreement);

export { MemoizedOnboardAgreement as OnboardAgreement };

【问题讨论】:

    标签: css react-native stylesheet touchableopacity react-native-stylesheet


    【解决方案1】:

    最简单的解决方案是将所有内容放在一个文本组件中。

    代码:

       <Text style={styles.termNconAgreement}>
          By clicking Sign Up, you acknowledge that you have read and agree to the
          <TouchableOpacity activeOpacity={0.4}>
            <Text style={styles.termNcon}>{' Terms & Conditions '}</Text>
          </TouchableOpacity>
          and
          <TouchableOpacity activeOpacity={0.4}>
            <Text style={styles.termNcon}> Privacy Policy</Text>
          </TouchableOpacity>
        </Text>
    

    输出:

    演示:

    https://snack.expo.io/HD8NeugqV

    【讨论】:

    • 我执行此操作时未显示条款和条件以及隐私政策。
    • @joyce 试用我的演示,它运行良好。也许您还有其他问题
    • 嗯,很有趣。我不知道它和你的演示有什么不同。
    • @joyce 尝试删除您的自定义样式(termNcon、termNconAgreement ...)。然后请再试一次。在我的演示中,我没有使用您的样式,可能就是这个问题。
    • @joyce 请更新您的问题并添加您当前的代码。
    【解决方案2】:

    您可以使用 Text 作为包装器而不是视图

      <Text style={styles.termNConWrapper}>
        <Text style={styles.termNconAgreement}>
          By clicking Sign Up, you acknowledge that you have read and agree to the
        </Text>
        <TouchableOpacity activeOpacity={0.4}>
          <Text style={styles.termNcon}>{' Terms & Conditions '}</Text>
        </TouchableOpacity>
        <Text style={styles.termNconAgreement}>and</Text>
        <TouchableOpacity activeOpacity={0.4}>
          <Text style={styles.termNcon}> Privacy Policy</Text>
        </TouchableOpacity>
        {/* <Text style={styles.termNconAgreement}>
          By clicking Sign Up, you acknowledge that you have read and agree to the
          <Text style={styles.termNcon}>{' Terms & Conditions '}</Text>
          and
          <Text style={styles.termNcon}> Privacy Policy</Text>
        </Text> */}
      </Text>
    

    无法检查样式,因为您有样式助手,但它应该可以工作。

    或者您可以使用 onPress of Text 组件代替 TouchableOpacity,这样您也可以更轻松地设置样式

     <Text onPress={()=>{alert(123)}} style={styles.termNcon}> Privacy Policy</Text>
    

    【讨论】:

    • 我执行此操作时未显示条款和条件以及隐私政策。
    • 你能分享你的自定义样式吗?
    • @joyce 更好地检查您的自定义样式助手和包装器的样式
    • 我尝试删除样式,但可触摸不透明度内的文本仍未显示。我正在使用 React Native 0.62.2
    • 另一种解决方案是使用文本的 onpress 而不是使用可触摸的不透明度 {alert(123)}} style={styles.termNcon}> 隐私政策 Text> 这也很有效,也很容易设置样式
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-08
    • 1970-01-01
    • 2019-08-23
    • 1970-01-01
    • 2021-08-17
    相关资源
    最近更新 更多