React-Native样式未应用

huangapple go评论42阅读模式
英文:

React-Native styles not getting appplied

问题

以下是您提供的代码的翻译部分:

// 在两个屏幕中未应用React-Native样式,已保存更改,但应用到一个屏幕后,另一个屏幕失去样式。以下是代码:

import { render } from 'react-dom';
import { NavigationContainer } from '@react-navigation/native';
import { TextInput } from 'react-native-gesture-handler';
const width = Dimensions.get('window').width;
const height = Dimensions.get('window').height;
styles = StyleSheet.create({
  Container: {
    flex: 1,
    width: '100%',
    width: width - 20,
    height: height - 10,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF'
  },
  ViewContainer: {
    width: width - 40,
    flex: 1,
    alignItems: 'center',
    backgroundColor: '#F5FCFF'
  },
  input: {
    borderBottomColor: '#F5FCFF',
    backgroundColor: '#FFFFFF',
    borderRadius: 30,
    borderBottomWidth: 1,
    paddingLeft: 8,
    width: 250,
    height: 45,
    marginBottom: 8,
    marginTop: 20,
    flexDirection: 'row',
    alignItems: 'center',
    marginLeft: 40
  },
  Button: {
    width: "80px",
    height: "30px",
    margin: 100,
    justifyContent: 'flex-end',
    alignItems: "flex-end",
  },
  buttonContainer: {
    height: 65,
    flexDirection: 'row',
    justifyContent: 'center',
    alignItems: 'center',
    marginBottom: 20,
    width: 250,
    borderRadius: 30,
    marginLeft: 70
  },
  Container: {
    flex: 1,
    justifyContent: "flex-end",
    alignItems: "flex-end",
  },
})


// 另一个屏幕的代码:

import AsyncStorage from '@react-native-async-storage/async-storage';
import { render } from 'react-dom';
import { NavigationContainer } from '@react-navigation/native';
import { TextInput } from 'react-native-gesture-handler';
import { getPermissionsAsync } from 'expo-location';
const width = Dimensions.get('window').width;
const height = Dimensions.get('window').height;

styles = StyleSheet.create({
  Container: {
    flex: 1,
    width: width - 20,
    height: height - 30,
    alignItems: 'center',
    backgroundColor: '#F5FCFF'
  },
  input: {
    marginTop: height / 4,
    margin: 20,
    borderBottomColor: '#F5FCFF',
    backgroundColor: '#FFFFFF',
    borderRadius: 30,
    borderBottomWidth: 1,
    width: 250,
    height: 45,
    marginBottom: 20,
    flexDirection: 'row',
    alignItems: 'center',
    marginLeft: 70
  },
  Button: {
    height: 45,
    flexDirection: 'row',
    justifyContent: 'center',
    alignItems: 'center',
    marginBottom: 20,
    width: 250,
    borderRadius: 60,
    borderBottomWidth: 1,
    marginTop: 90,
    marginLeft: 70
  },
  Button2: {
    flex: 1,
    height: 20,
    borderRadius: 60,
    borderBottomWidth: 1,
    width: 30,
    color: '#f0f8ff',
    alignSelf: 'flex-end',
    borderStyle: 'dashed'
  }
})


// 使用堆栈导航,可能是问题所在,以下是代码:

function Tabnav() {

  return (
    <Tab.Navigator
      screenOptions={{
        tabBarActiveTintColor: '#101010',
        TabBarLabelStyle: {
          fontSize: 15,
        },
        TabBarStyle: {
          backgroundColor: '#ffd700',
        }
      }}
    >
      <Tab.Screen name="Homescreen" component={Home}
        options={{
          headerShown: false,
          tabBarIcon: ({ color, size }) => (
            <Ionicons name="ios-home" color={color} size={size} />
          )
        }} />

      <Tab.Screen name="Perfil" component={userProfile}
        options={{
          headerShown: false,
          tabBarIcon: ({ color, size }) => (
            <Ionicons name="ios-person" size={size} color={color} />
          )
        }} />

    </Tab.Navigator>

  );
}

export default function App() {

  return (

    <NavigationContainer initialRouteName="Signin">
      <Stack.Navigator>
        <Stack.Screen name="Signin" component={Signin} />
        <Stack.Screen name="Register" component={Register} />
        <Stack.Screen name="Forgotpass" component={Forgotpass} />
        <Stack.Screen name="Reset" component={Reset} />
        <Stack.Screen name="Home" component={Tabnav} options={{ headerLeft: () => null, gestureEnabled: false, }} />
        <Stack.Screen name="Dermatologistas" component={Dermatologistas} />
        <Stack.Screen name="Hospitais" component={Hospitais} />
        <Stack.Screen name="Cardiologista" component={Cardiologista} />
        <Stack.Screen name="Pneumologista" component={Pneumologista} />
        <Stack.Screen name="Odontologo" component={Odontologo} />
        <Stack.Screen name="Oftalmologista" component={Oftalmologista} />
        <Stack.Screen name="UserProfile" component={userProfile} />
        <Stack.Screen name="Profile" component={Profile} />

        <Stack.Screen name="Edit" component={Edit} />
      </Stack.Navigator>

    </NavigationContainer>

  );
}

希望这有助于您的问题。如果需要进一步的帮助,请告诉我。

英文:

React-Native styles are not getting applied in two screens, I already saved the changes but when it gets applie to one screen, the other one loses it's style. Here is the code :

import { render } from &#39;react-dom&#39;;
import {NavigationContainer} from &#39;@react-navigation/native&#39;
import { TextInput } from &#39;react-native-gesture-handler&#39;;
const width = Dimensions.get(&#39;window&#39;).width
const heigth = Dimensions.get(&#39;window&#39;).height
styles = StyleSheet.create({
Container:{
flex:1,
width: &#39;100%&#39;,
width: width -20,
height:heigth - 10,
justifyContent: &#39;center&#39;,
alignItems: &#39;center&#39;,
backgroundColor: &#39;#F5FCFF&#39;
},
ViewContainer:{
width: width -40,
flex:1,
alignItems: &#39;center&#39;,
backgroundColor: &#39;#F5FCFF&#39;
},
input:{
borderBottomColor: &#39;#F5FCFF&#39;,
backgroundColor: &#39;#FFFFFF&#39;,
borderRadius:30,
borderBottomWidth: 1,
paddingLeft: 8,
width:250,
height:45,
marginBottom:8,
marginTop:20,
flexDirection: &#39;row&#39;,
alignItems:&#39;center&#39;,
marginLeft:40
},
Button:{
width:&quot;80px&quot;,
height:&quot;30px&quot;,
margin:100,
justifyContent:&#39;flex-end&#39;,
alignItems: &quot;flex-end&quot;,
},
buttonContainer: {
height:65,
flexDirection: &#39;row&#39;,
justifyContent: &#39;center&#39;,
alignItems: &#39;center&#39;,
marginBottom:20,
width:250,
borderRadius:30,
marginLeft:70
},
Container:{
flex:1,
justifyContent:&quot;flex-end&quot;,
alignItems: &quot;flex-end&quot;,
},
})

and the other one here:

import AsyncStorage from &#39;@react-native-async-storage/async-storage&#39;;
import { render } from &#39;react-dom&#39;;
import {NavigationContainer} from &#39;@react-navigation/native&#39;
import { TextInput } from &#39;react-native-gesture-handler&#39;;
import { getPermissionsAsync } from &#39;expo-location&#39;;
const width = Dimensions.get(&#39;window&#39;).width
const heigth = Dimensions.get(&#39;window&#39;).height
styles = StyleSheet.create({
Container:{
flex:1,
width: width -20,
height:heigth-30,
alignItems: &#39;center&#39;,
backgroundColor: &#39;#F5FCFF&#39;
},
input:{
marginTop: heigth/4,
margin: 20,
borderBottomColor: &#39;#F5FCFF&#39;,
backgroundColor: &#39;#FFFFFF&#39;,
borderRadius:30,
borderBottomWidth: 1,
width:250,
height:45,
marginBottom:20,
flexDirection: &#39;row&#39;,
alignItems:&#39;center&#39;,
marginLeft:70
},
Button:{
height:45,
flexDirection: &#39;row&#39;,
justifyContent: &#39;center&#39;,
alignItems: &#39;center&#39;,
marginBottom:20,
width:250,
borderRadius:60,
borderBottomWidth: 1,
marginTop:90,
marginLeft:70
},
Button2:{
flex:1,
height:20,
borderRadius:60,
borderBottomWidth: 1,
width:30,
color:&#39;#f0f8ff&#39;,
alignSelf:&#39;flex-end&#39;,
borderStyle: &#39;dashed&#39;
}
})

I'm using stack navigation and think that it migth be the problem, here is the code:
function Tabnav() {

  return(
&lt;Tab.Navigator 
screenOptions={{
tabBarActiveTintColor: &#39;#101010&#39;,
TabBarLabelStyle: {
fontSize: 15,
},
TabBarStyle: {
backgroundColor: &#39;#ffd700&#39;,
}
}}
&gt;
&lt;Tab.Screen name=&quot;Homescreen&quot; component={Home} 
options={{
headerShown: false,
tabBarIcon: ({ color, size }) =&gt; (
&lt;Ionicons name=&quot;ios-home&quot; color={color} size={size} /&gt;
)
}}/&gt;
&lt;Tab.Screen name=&quot;Perfil&quot; component={userProfile}
options={{
headerShown: false,
tabBarIcon: ({ color, size }) =&gt; (
&lt;Ionicons name=&quot;ios-person&quot; size={size} color={color} /&gt;
)
}}/&gt;
&lt;/Tab.Navigator&gt; 
);
}
export default function App(){
return (
&lt;NavigationContainer initialRouteName=&quot;Signin&quot;&gt;
&lt;Stack.Navigator&gt;
&lt;Stack.Screen name=&quot;Signin&quot; component={Signin} /&gt;
&lt;Stack.Screen name=&quot;Register&quot; component={Register}/&gt;
&lt;Stack.Screen name=&quot;Forgotpass&quot; component={Forgotpass}/&gt;
&lt;Stack.Screen name=&quot;Reset&quot; component={Reset}/&gt;
&lt;Stack.Screen name=&quot;Home&quot; component={Tabnav} options={{headerLeft:()=&gt;null, gestureEnabled: false,}}/&gt;
&lt;Stack.Screen name=&quot;Dermatologistas&quot; component={Dermatologistas}/&gt;
&lt;Stack.Screen name=&quot;Hospitais&quot; component={Hospitais}/&gt;
&lt;Stack.Screen name=&quot;Cardiologista&quot; component={Cardiologista}/&gt;
&lt;Stack.Screen name=&quot;Pneumologista&quot; component={Pneumologista}/&gt;
&lt;Stack.Screen name=&quot;Odontologo&quot; component={Odontologo}/&gt;
&lt;Stack.Screen name=&quot;Oftalmologista&quot; component={Oftalmologista}/&gt;
&lt;Stack.Screen name=&quot;UserProfile&quot; component={userProfile}/&gt;
&lt;Stack.Screen name=&quot;Profile&quot; component={Profile}/&gt;
&lt;Stack.Screen name=&quot;Edit&quot; component={Edit}/&gt;
&lt;/Stack.Navigator&gt;
&lt;/NavigationContainer&gt;
);
}

答案1

得分: 3

需要从react-native导入StyleSheet。

import { StyleSheet, View, Text } from "react-native";
英文:

Need to import StyleSheet from react-native

import {StyleSheet, View, Text} from &quot;react-native&quot;;

答案2

得分: 1

You have forgotten the const keyword.
const styles = StyleSheet.create({

You have forgotten to import StyleSheet, so this may be another problem.

If the StyleSheet is declared in a separate file, you must export it like this:

export default styles or export {styles}

To use styles in other files, if you do export default styles like this, then import like this:

import styles from "../your/directory/to/styles/"

If you export like this:

export {styles}

Then import like this:

import {styles} from "../your/directory/to/styles/"

英文:

you have for got the const keyword.
const styles = StyleSheet.create({

you have forgot to import StyleSheet so this may be another Problem.

if the StyleSheet is declare in separate file you must have to export it like this

export default styles or export {styles}

to use styles in the other files . if you do export default styles like this then import like this

import styles from &quot;../your/directroy/to/styles/&quot;

if you export like this

export {styles}

then import like this

import {styles} from &quot;../your/directroy/to/styles/&quot;

答案3

得分: 0

"just needed to define a const in the stylesheet :)\n\nstyles = StyleSheet.create({})"

英文:

just needed to define a const in the stylesheet React-Native样式未应用

styles = StyleSheet.create({})

huangapple
  • 本文由 发表于 2023年6月9日 06:11:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436018.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定