css 在React Native中将项目左右对齐

vmdwslir  于 2022-11-19  发布在  React
关注(0)|答案(1)|浏览(186)

如何在React Native中将项目左右对齐?
无论我尝试如何组合flexbox属性,这两组文本都像这样彼此相邻:

如何使“登录”位于屏幕右侧,而存储库位于左侧?
下面是我的代码:

import { View, StyleSheet, Text, ScrollView } from 'react-native';
import Constants from 'expo-constants';
import { Link } from "react-router-native";

const styles = StyleSheet.create({
  container: {
    paddingTop: Constants.statusBarHeight,
    paddingLeft: 10,
    paddingBottom: 20,
    backgroundColor: 'grey',
  },
  text: {
    fontSize: 20,
    color: 'white',
    fontWeight: 'bold'
  },
  linkText: {
    color: 'white',
  },
  nesteddivleft: {
    flex: 1,          
    display: "flex",
    justifyContent: "flex-start",
    alignItems: "center",
  },
  nesteddivright: {
    flex: 1,
    display: "flex",
    justifyContent: "flex-end",
    alignItems: "center",
  },
  scrollbar: {
display: 'flex',
justifyContent: 'space-between'
  }
});

const AppBar = () => {
  return <><View style={styles.container}>
    <ScrollView style="scrollview" horizontal>
    <View style={styles.nesteddivleft}><Link to="/"><Text style={styles.text}>Repositories</Text></Link></View>
    <View style={styles.nesteddivright}><Link to="/signin"><Text style={styles.linkText}>Sign In</Text></Link></View>
    </ScrollView>
    </View>
    </>
};

export default AppBar;

应用程序:

import Main from './src/components/Main'
import { StatusBar } from 'expo-status-bar';
import { NativeRouter } from 'react-router-native';

export default function App() {

  return (
<> 
     <NativeRouter>
     <Main/>
     </NativeRouter>
     <StatusBar style="auto" />
</>
  );
}

如您所见,我尝试将justify-content:父div上的空格,这样就不会执行任何操作。
我也尝试过这个解决方案,但它没有任何作用:Aligning elements left, center and right in flexbox

1dkrff03

1dkrff031#

import { View, StyleSheet, Text, ScrollView } from 'react-native';
import Constants from 'expo-constants';
import { Link } from "react-router-native";

const styles = StyleSheet.create({
  container: {
    paddingTop: Constants.statusBarHeight,
    paddingLeft: 10,
    paddingBottom: 20,
    backgroundColor: 'grey',
    display: "flex",
justifyContent: "space-between",
minWidth: '100%',
flexDirection: "row"
  },
  text: {
    fontSize: 20,
    color: 'white',
    fontWeight: 'bold'
  },
  linkText: {
    color: 'white',
  },
  nesteddivleft: {
  },
  nesteddivright: {
  },
  scrollbar: {

  }
});

const AppBar = () => {
  return <><View style={styles.container}>
  
    <View style={styles.nesteddivleft}><Link to="/"><Text style={styles.text}>Repositories</Text></Link></View>
    <View style={styles.nesteddivright}><Link to="/signin"><Text style={styles.linkText}>Sign In</Text></Link></View>
 
    </View>
    </>
};

export default AppBar;

解决问题的步骤:
1.取出scrollview组件。
1.将“显示”设置为“弹性”,将“内容”设置为“间距”,将“最小宽度”设置为100%,并将“方向”设置为“行”。
不是最佳的,因为我需要在那里的Scrollview组件,以及,我将需要找到一个解决方案,让我有该组件作为一个孩子。

相关问题