【问题标题】:React Native Text color not working反应本机文本颜色不起作用
【发布时间】:2019-01-10 19:20:39
【问题描述】:

我在 TouchableOpacity 中有一个 Text 组件,我想根据 var 更改颜色。

这是我的代码:

import React, { Component } from "react";
import { StyleSheet, Text, View, TouchableOpacity } from "react-native";

var flag = false;

export default class MyTest extends Component {
  changeColor() {
    flag = true;
  }

  render() {
    return (
      <View style={styles.container}>
        <TouchableOpacity
          style={{ flex: 1, backgroundColor: "#888888", margin: 20 }}
          onPress={this.changeColor.bind(this)}
        >
          <Text style={[{ color: "blue" }, flag ? { color: "red" } : false]}>
            One
          </Text>
        </TouchableOpacity>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    backgroundColor: "#F5FCFF"
  }
});

我尝试使用this.state.textColor,但没有结果。我也尝试在样式变量中使用它,但同样不起作用。

有什么想法吗?

【问题讨论】:

    标签: javascript android reactjs react-native


    【解决方案1】:

    flag 变量不在你的组件状态,所以当它改变时组件不会重新渲染。

    改为将其置于您的组件状态并使用setState 切换它,它将起作用。

    class MyTest extends Component {
      state = { flag: true };
    
      changeColor = () => {
        this.setState(previousState => {
          return { flag: !previousState.flag };
        });
      };
    
      render() {
        return (
          <View style={styles.container}>
            <TouchableOpacity
              style={{ flex: 1, backgroundColor: "#888888", margin: 20 }}
              onPress={this.changeColor}
            >
              <Text style={{ color: this.state.flag ? "red" : "blue" }}>One</Text>
            </TouchableOpacity>
          </View>
        );
      }
    }
    

    【讨论】:

      【解决方案2】:

      您必须为 Text 赋予样式以获取颜色。

       <Text style={styles.steelblue}>Sign Up</Text>
      

      将此样式赋予文本。

      const styles = StyleSheet.create({
        steelblue: {
          color: "steelblue",
        },
      });
      

      【讨论】:

        【解决方案3】:

        试试这个例子,这可能会帮助你解决问题。

        import React, { Component } from 'react';
        import { Platform, StyleSheet, View, Text } from 'react-native';
        
        
        export default class App extends Component {
        
          render() {
        
            return (
              <View style={styles.container}>
                <Text style={[styles.setFontSize,styles.setColorRed]}> React Native Font example 1</Text>
                <Text style={[styles.setFontSize,styles.setColorPink]}> React Native Font example 2</Text>
                <Text style={[styles.setFontSize,styles.setColorPurple]}> React Native Font example 3</Text>
                <Text style={[styles.setFontSize,styles.setColorBlue]}> React Native Font example 4</Text>
              </View>
            );
          }
        }
        
        
        const styles = StyleSheet.create({
          container: {
            flex: 1,
            justifyContent: 'center',
            alignItems: 'center'
          },
          setFontSize: {
            fontSize: 20,
            fontWeight : 'bold' 
          },
          setColorRed : {
            color: '#f44336'
          },
          setColorPink :{
            color: '#e91e63'
          },
          setColorPurple :{
            color: '#9c27b0'
          },
          setColorBlue :{
            color: '#2196f3'
          },
        });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-03-15
          • 1970-01-01
          • 2016-07-20
          • 2018-04-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多