【问题标题】:React Native: ScrollView and Layout Elements with flexReact Native:带有 flex 的 ScrollView 和布局元素
【发布时间】:2021-01-28 05:06:43
【问题描述】:

您好,我正在尝试添加一个 ScrollView,但它没有像预期的那样工作。

如果我将 flex:1 添加到 ScrollView,那么我可以设置子元素的样式,但没有滚动条,所以我无法滚动。 如果我删除 flex:1 或将其更改为 flexGrow 则我有滚动条,但子元素的样式已损坏。

如何将 i ScrollView 和样式添加到孩子?

我需要一些信息为什么会发生。谢谢。

import React, { useContext, useState, useEffect } from 'react';
import {
  Text,
  TextInput,
  StyleSheet,
  View,
  TouchableOpacity,
  Alert,
  ScrollView,
} from 'react-native';

import { globalStyles } from '../styles/global';
import colors from '../styles/color';

import FontAwesome from 'react-native-vector-icons/FontAwesome';
import Feather from 'react-native-vector-icons/Feather';
import * as Animatable from 'react-native-animatable';

import Button from '../components/Button';



const SignInScreen = (props) => {


  return (
    <View style={styles.container}>
      <ScrollView contentContainerStyle={styles.scrollViewContainer}>
        <View style={styles.containerTop}>
          <Text style={globalStyles.h1}>Login</Text>
        </View>
        <View style={styles.containerBottom}>
          <View style={styles.viewAction}>
            <FontAwesome name='user-o' color='#ccc' size={20} style={{ width: 20 }} />

            <TextInput
              style={[styles.input]}
              placeholder="Your Email"
              value={data.email}
              autoCapitalize='none'
              onChangeText={(val) => textInputEmailChange(val)}
            />

          </View>

          <View style={styles.viewAction}>
            <FontAwesome name='lock' color='#ccc' size={20} style={{ width: 20 }} />

            <TextInput
              style={[styles.input]}
              placeholder="Password"
              value={data.password}
              autoCapitalize='none'
              secureTextEntry={data.secureTextEntry ? true : false}
              onChangeText={(val) => handlePasswordChange(val)}
            />

            <TouchableOpacity onPress={updateSecureTextEntry}>
              {data.secureTextEntry ? <Feather name='eye-off' color='#ccc' size={20} style={{ width: 20 }} />
                : <Feather name='eye' color='#80c904' size={20} style={{ width: 20 }} />
              }
            </TouchableOpacity>

          </View>

          {uiErrors.errors ? (<Text style={styles.errorMsg}>{uiErrors.errors}</Text>) : (<Text></Text>)}

          <Button
            content={'Login'}
            buttonStyles={{ marginVertical: 20, paddingVertical: 15, }}
            onPress={handleLogin}
          />
          <Button
            content={'Login'}
            buttonStyles={{ marginVertical: 20, paddingVertical: 15, }}
            onPress={handleLogin}
          />

          <Button
            content={'Login'}
            buttonStyles={{ marginVertical: 20, paddingVertical: 15, }}
            onPress={handleLogin}
          />

          <Button
            content={'Login'}
            buttonStyles={{ marginVertical: 20, paddingVertical: 15, }}
            onPress={handleLogin}
          />
          <Button
            content={'Login'}
            buttonStyles={{ marginVertical: 20, paddingVertical: 15, }}
            onPress={handleLogin}
          />

          <Button
            content={'Create an account'}
            buttonStyles={{ backgroundColor: 'transparent' }}
            textStyles={styles.signUpBtnText}
            onPress={() => { navigation.navigate('SignUp'); }}
          />

        </View>
      </ScrollView>
    </View>
  )
}

const styles = StyleSheet.create({

  container: {
    flex: 1,
    backgroundColor: colors.light_theme.splashgb,
  },
  scrollViewContainer: {
    // flex: 1,
    // flexGrow: 1,
    // justifyContent: 'space-between'
  },
  containerTop: {
    flex: 1, // it add this only if flex:1 is added to scrollViewContainer but the then i can't scroll.
    padding: 25,
    backgroundColor: 'blue'
  },
  containerBottom: {
    flex: 1,
    padding: 25,
    flexDirection: 'column',
    backgroundColor: colors.light_theme.container,
    borderTopLeftRadius: 25,
    borderTopRightRadius: 25,
  },

  viewAction: {
    flexDirection: 'row',
    width: '100%',
    borderBottomWidth: 1,
    borderBottomColor: colors.light_theme.lightgray,
    paddingVertical: 0,
    alignItems: 'center',
  },

  input: {
    flex: 1,
    padding: 15,
  },

  text: {
    color: colors.light_theme.white,
  },
  errorMsg: {
    color: colors.light_theme.errorMsg,
    fontSize: 14,
    marginVertical: 5,
  },

  signUpBtnText: {
    color: colors.light_theme.primary
  }

});

export default SignInScreen;

【问题讨论】:

    标签: react-native flexbox scrollview


    【解决方案1】:

    1:将您的 ScrollView 放入 View:

    ... <View><ScrollView> ... </ScrollView></View> ...
    

    2:从你的 scrollViewContainer 样式中删除“flex:1”,这样它就只适用于 fexGrow 和 justifyContent

    【讨论】:

    • 它已经被包裹在一个视图中......或者你是什么意思?
    • 在我的项目中,我在顶部有一个列表,我想在其中滚动,最后有一个按钮,我想像页脚一样操作。所以结果是这样的:&lt;View style={{flex: 1}}&gt; &lt;View style={{flex: 9}}&gt; &lt;ScrollView style={{flexGrow: 1, justifyContent: "center"&gt; MY List &lt;ScrollView /&gt; &lt;/View&gt; &lt;View style={{flex: 1}}&gt; My Button &lt;/View&gt; &lt;/View&gt;
    猜你喜欢
    • 2019-09-20
    • 1970-01-01
    • 1970-01-01
    • 2019-01-08
    • 2018-12-23
    • 1970-01-01
    • 2021-02-09
    • 1970-01-01
    • 2015-09-12
    相关资源
    最近更新 更多