我是一名新手,正在try 学习如何正确定位组件之间的位置.目前,我做了一个我想要的模式,让它出现在任何背景上.如果我采用制作白色方框的视图,它确实出现在中间.

我想要渲染屏幕上的所有内容:

        <View
            style={Styles.background}>

            <View
                style={{justifyContent:'flex-start', alignItems:'center'}}
            >
                <View
                    style={{
                        backgroundColor:Colors.gtechWhite,
                        width:'80%',
                        height:'25%'
                    }}
                >
                </View>

            </View>

            <View
                style={{flex:1, justifyContent:'center', alignItems:'center'}} >
                {addExerciseModalOpen && <AddExerciseModal setAddExerciseModalOpen={setAddExerciseModalOpen}/>}
            </View>
            <View style={{ position:'absolute', right:'6%', bottom:'14%', alignItems:'flex-end'}} >
                {filterModalOpen && <FilterModal selectedFilter={selectedFilter} setSelectedFilter={setSelectedFilter}  />}
            </View>
            <View style={{position:'absolute', right:'6%', bottom:'8%', alignItems:'flex-end', flexDirection:'row'}}>
                <Pressable
                    style={{paddingRight:35}}
                    onPress={()=>{
                        Haptics.notificationAsync(
                            Haptics.NotificationFeedbackType.Success
                        )
                        setAddExerciseModalOpen(false)
                        setFilterModalOpen(!filterModalOpen);
                    }}
                >
                    <Octicons name="filter"  size={40} color={Colors.darkMode.button} />
                </Pressable>
                <Pressable
                    onPress={()=>{
                        Haptics.notificationAsync(
                            Haptics.NotificationFeedbackType.Success
                        )
                        setFilterModalOpen(false)
                        setAddExerciseModalOpen(!addExerciseModalOpen);

                    }}
                >
                    <Ionicons name="add-circle-outline" size={40} color={Colors.darkMode.button}/>
                </Pressable>
            </View>
        </View>

我想要在中间渲染的组件:

<View style={{
                flexDirection:'row',
                position: 'absolute',
                justifyContent: 'flex-start',
                alignItems: 'flex-start',
                width: '80%',
                height: '34%',
                borderRadius: 25,
                backgroundColor: Colors.darkMode.cards1,
                overflow: 'hidden' }}>
                <View style={{flex:1, top:10}}>
                    <View style={{ alignItems: 'center', paddingBottom:25}} >
                        <Text style={Styles.modalTitleText}>Add An Exercise</Text>

                        <TextInput
                            placeholder="Exercise Name"
                            placeholderTextColor="#ffffff"
                            style={{
                                color:'white',
                                fontFamily:'RobotoSlab-Bold',
                                fontSize:15,
                                marginLeft:25,
                                width:'90%',
                                marginTop:25,
                                height:45}}
                        />
                        <WhiteDivider dividerWidth={90} dividerMarginVertical={0}/>
                        <View style={{
                            flexDirection:'row',
                            flexGrow:1,
                            paddingTop:25,
                            marginRight:15,
                            marginLeft:15,
                        }}>
                            <ScrollView
                                style={{width:'100%'}}
                                horizontal={true}
                                showsHorizontalScrollIndicator={false}
                            >
                                {ExerciseCategories()}
                            </ScrollView>
                        </View>
                        <View style={{
                            flexDirection:'row',
                            flexGrow:1,
                            paddingTop:15,
                            marginRight:15,
                            marginLeft:15,
                        }}>
                            <Pressable
                                style={{alignItems:'center', backgroundColor:Colors.gtechWhiteTransparent, borderRadius:9, width:'95%', paddingVertical:4 }}
                                onPress={()=>setAddExerciseModalOpen(false)}
                            >
                                <Text style={{
                                    color: Colors.gtechGold,
                                    fontSize: 17,
                                    fontFamily: 'RobotoSlab-Medium'}}
                                >
                                    CANCEL
                                </Text>
                            </Pressable>
                        </View>
                        <View style={{
                            flexDirection:'row',
                            flexGrow:1,
                            paddingTop:15,
                            marginRight:15,
                            marginLeft:15,
                        }}>
                            <Pressable
                                style={{alignItems:'center', backgroundColor:Colors.gtechGoldTransparent, borderRadius:9, width:'95%', paddingVertical:4 }}
                                onPress={()=>setAddExerciseModalOpen(false)}
                            >
                                <Text style={{
                                    color: Colors.gtechGold,
                                    fontSize: 17,
                                    fontFamily: 'RobotoSlab-Medium'}}
                                >
                                    SAVE EXERCISE
                                </Text>
                            </Pressable>
                        </View>

                    </View>
                </View>
            </View>

enter image description here

推荐答案

通过使用这一点,你可以将莫代尔带到中心.

   style:{
    flex:1,
    justifyContent: 'center',
    alignItems: 'center'
  }

示例:

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

export default function App() {
  return (
      <Modal
        animationType={"slide"}
        transparent={true}
        visible={true}
        onRequestClose={this.closeModal}>
        <View style={styles.centered}>
          <View style={styles.childStyle}>
            <Text style={styles.title}>Center a View Component</Text>
            <Text style={styles.subtitle}>Using Flexbox</Text>
          </View>
        </View>
      </Modal>
  );
}

const styles = StyleSheet.create({
  centered: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
    backgroundColor: "#ffc2c2",
  },
  title: {
    fontSize: 18,
    marginVertical: 2,
  },
  subtitle: {
    fontSize: 14,
    color: "#888",
  },
});

React-native相关问答推荐

React Native 在每个页面加载时调用数据库

一旦我关闭应用程序,Json struct 就会发生变化

如何在react native 中从 axios 重定向并将数据传递给路由

React-Native-Web 错误:rnw_blogpost.bundle.js:1414 Uncaught TypeError: Cannot read properties of undefined (reading 'isBatchingLegacy')

调用 ES6 方法时绑定上下文.如何从称为回调的方法中访问对象?

在 React Native Paper 中更改 TextInput 的文本 colored颜色

如何为我的 React Native Android 应用程序使用 Crashlytics?

请求无法完成,因为您已超出配额

React Native:约束 Animated.Value

我可以在 react-native 中使用 reactJS 库吗?

React Native 使视图 Hug键盘顶部

xcode 8.3 框架未找到架构 armv7 的 FileProvider

如何使 React Native Animated.View 可点击?

react-native run-android 构建错误':app:generateDebugBuildConfig'

在 React Native 中,如何使用浅渲染测试我的组件?

使用 React Navigation 导航堆栈时重新渲染组件

React Native如何将this.setState更改传递给父级

使用 React Native 一次启动多个 Animated.timing

在 React Native 中将props传递给外部样式表?

使用行数react-native文本组件