css - 如何在同一容器中将标题居中对齐,文本居左对齐?

标签 css react-native

您好,我正在学习 React Native。我正在尝试创建注册表单。我需要中间顶部的标题和左侧的所有标签字段。以下是我的代码。

 <ScrollView contentContainerStyle={{ flexGrow: 1 }}
    scrollEnabled >
  <View style={styles.fieldsContainer}>
  <View>
  <Text style={styles.headline}>Personal Info</Text>
  </View>
 <Text></Text>

和CSS

    const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#F5FCFF',
    alignItems: 'stretch',
  },
fieldsContainer: {
    flex: 1,
    margin:10,
    paddingTop:30,
    paddingLeft:20,
    backgroundColor: '#D3D3D3',
    alignItems: 'stretch',
    alignItems: 'center',
  },
  textLabelsStyle: {
fontSize:34,
fontWeight:'bold',
color:'black',
  },
  headline: {
    textAlign: 'center', 
    fontWeight: 'bold',
    fontSize: 18,

    marginTop: 0,
    width: 200,
    backgroundColor: 'yellow',
  },

我面临的问题是我无法同时将标题置于中心和将字段标签置于左侧。

最佳答案

将 alignSelf 添加到中心。

<ScrollView contentContainerStyle={{ flexGrow: 1 }}
    scrollEnabled >
  <View style={styles.fieldsContainer}>
  <View style={{alignSelf:'center'}}>
  <Text style={styles.headline}>Personal Info</Text>
  </View>
 <Text></Text>

关于css - 如何在同一容器中将标题居中对齐,文本居左对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54626546/

相关文章:

html - CSS - 使图像适合特定大小的 div

html - 输入类型文本宽度在 chrome 和 firefox 中不同

html - 如何在两个并排的 div 中垂直居中放置文本?

javascript - Bootstrap 没有响应,但我有元标记

react-native - 在 macOS Big sur 上安装 pod 失败

javascript - 如何防止屏幕滚动到 x :0, y :0 when an input loses focus?

CSS 间距问题

react-native - 如何在React Native中使用react-native-image-picker显示视频和图像

android - 在 React Native 中使用 ExoPlayer

android - 如何在 react-native android 应用程序中显示 GIF?