嗨,我想在文本中包装TouchableOpacity,因为我想让一些文本可以点击.当我把所有东西都包装在一个文本中时,它看起来很完美,这就是我想要它的样子.

 <Text 
   style={{color: colors.black,
           fontSize: 12,
           fontWeight: 'normal',
           marginTop: 10,
           lineHeight: 18}}>
      {strings.loginPrivacyTermsCondOne} 
        <Text style={{color: colors.primaryColor,
                      fontSize: 12,
                      fontWeight: 'normal',}}>
          {strings.loginPrivacyTermsCondTwo}
        </Text>
          {strings.loginPrivacyTermsCondThree} 
        <Text style={{color: colors.primaryColor,
                      fontSize: 12,
                      fontWeight: 'normal'}}>
          {strings.loginPrivacyTermsCondFour}
        </Text>

          {/* <TouchableOpacity onPress={ this.termsOfService }>
            <Text style={{color: colors.primaryColor,
                          fontSize: 12,
                          fontWeight: 'normal',}}>
              {strings.loginPrivacyTermsCondFour}
            </Text>
          </TouchableOpacity> */}
      </Text>

当我添加TouchableOpacity时,它不起作用.

我试着把它添加到一个视图中,然后效果很好,我可以添加TouchableOpacity,但从UI的Angular 来看,它们没有正确对齐.

这是一个屏幕截图,显示了TouchableOpacity不起作用的文本,第二位在视图中,TouchableOpacity起作用,但看起来不正确.

enter image description here

如何才能让它看起来像第一位.非常感谢您的建议.

谢谢

推荐答案

您可以嵌套文本元素,并 for each 要使其可按的嵌套文本元素(链接)分配onPress处理程序.

见下文.有一个外部文本元素,内部是另一个文本元素,子文本元素有一个onPress处理程序,如果你运行这个,你会看到"但是这是!"在按下onPress处理程序时执行它,不需要touch *元素.

<Text style={{color: '#000'}}> 
     This part is not clickable 
     <Text onPress={() =>
           {alert('but this is');}}
           style={{color: '#00F'}}>
     But this is!
     </Text> 
     but this isn't
</Text>

您可以创建一个样式,将其放置在具有/a onPress处理程序的任何文本元素上,以使它们具有不同的 colored颜色 ,如示例图像中所示.

注意,这很像HTML,例如,你可以在p元素中嵌套锚标记;

<p>
    This part is not clickable 
    <a href="https://google.com"> but this is</a>
    but this isn't
</p>

在你的例子中,是这样的(未经测试):

<Text style={{color: colors.black,
              fontSize: 12,
              fontWeight: 'normal', 
              marginTop: 10,
              lineHeight: 18}}>
        {strings.loginPrivacyTermsCondOne} 
        <Text style={{color: colors.primaryColor,
                      fontSize: 12,
                      fontWeight: 'normal',}}>
          {strings.loginPrivacyTermsCondTwo}
        </Text>
          {strings.loginPrivacyTermsCondThree} 
        <Text style={{color: colors.primaryColor, 
                      fontSize: 12,
                      fontWeight: 'normal'}}>
          {strings.loginPrivacyTermsCondFour}
        </Text>

          <Text onPress={ this.termsOfService } 
                style={{color: colors.primaryColor,
                        fontSize: 12, 
                        fontWeight: 'normal'}}>
              {strings.loginPrivacyTermsCondFour}
          </Text> 
      </Text>

为了回应你的 comments ,这里有一个在点击链接后改变 colored颜色 的例子.

简而言之,我在state上添加了一个布尔字段,一旦文本被按下,我会将该state变量更新为true,然后文本元素的style值有一个三元运算符,它决定文本的渲染 colored颜色 ,在我的示例中,它将显示为"colors".如果尚未按下primaryColor,则为"红色",单击后为"红色".

class Foo extends Component {

    constructor (props) {
        super(props);

        this.state = {
            privacyClicked: false //Track if they have clicked privacy
        };
    }

    render () {

        return (
             <Text onPress={ () =>{
    this.setState({
        privacyClicked: true
    });
}} style={{color: (this.state.privacyClicked ? colors.primaryColor : 'red'), 
           fontSize: 12,
           fontWeight: 'normal'}}>
    {strings.loginPrivacyTermsCondFour}
</Text> 
        );
    }
}

PS,示例文本的格式设置不是很好.

React-native相关问答推荐

错误:HostBody::get for prop NativeUnimoduleProxy中出现异常- Android虚拟设备(AVD)使用Expo测试React Native App时崩溃

Firebase身份验证Redux使用说明

为什么当我更换屏幕时playground 音乐不会停止?

按下按钮有一个borderRadius

react-native-snap-carousel 无法正确渲染

如何 break .map 功能

React Native:约束 Animated.Value

Firebase JavaScript SDK 和 react-native-firebase 有什么区别

如何从 React Native-App 中的 res/drawable-folder 加载图像?

当我导航到react-native 的新页面时,如何强制卸载组件?

在 Docker 中使用 Fastlane 构建 iOS 应用

Rich ReactNative TextInput

如何在 react-native 中从 AsyncStorage 中删除元素

未计算文件的 React-Native Bundle Error 错误 SHA-1

如何将 Tensorflow 与 react-native 一起使用?

React Nativedropped so far性能监视器计数不断增加

是否可以在 react-native 的构造函数中调用异步函数?

我可以从react-native元素中删除 tvOS 吗?

INSTALL_FAILED_UPDATE_INCOMPATIBLE:包签名与之前安装的版本不匹配,忽略

我将如何根据百分比为按钮的宽度设置动画,并且它的背景 colored颜色 也是如此?