标题标题不使用本机库在React Native中居中

时间:2017-08-11 09:58:48

标签: react-native native-base

我正在使用native-base创建React Native应用程序:

我正在使用Header Component来显示Body,Left和Right元素。根据文档,标题应该自动居中,但不是(如下所示)。

我在这里错过了一些简单的东西吗?任何帮助将不胜感激!

Area

enter image description here

7 个答案:

答案 0 :(得分:7)

如果您希望标题位于中心,可以像这样在左侧,正文和右侧添加flex:1

    return(
        <Container style={styles.container}>
            <Header style={styles.header}>
                <Left style={{flex:1}}>
                    <Icon name='arrow-back' />
                </Left>
                <Body style={{flex:1}}>
                    <Title>Seminars</Title>
                </Body>
                <Right style={{flex:1}}>
                    <Icon name='menu' />
                </Right>
            </Header>
            <Content contentContainerStyle={styles.content} >
                <Text>Content Here</Text>
            </Content>
        </Container>
    )

这就是结果:

enter image description here

我希望这个答案可以帮助你:)

答案 1 :(得分:4)

这个答案可以帮助您,为我工作。

        <Header style={{backgroundColor:'#ff2929'}}>
        <Left style={{flex: 1}}>
            <Button transparent style={{width: 65}}>
                <Icon style={{color:"#fff"}} type="MaterialIcons" name={this.props.imageLeft}/>
            </Button>
        </Left>
        <Body style={{flex: 3,justifyContent: 'center'}}>
        <Title style={{color: '#fff',alignSelf:'center'}}>{this.props.headerTitle}</Title>
        </Body>
        <Right style={{flex: 1}}>
            <Button transparent style={{width: 65}}>                    
                <Icon style={{color:this.props.color}} type="MaterialIcons" name={this.props.imageRight}/>
            </Button>
        </Right>
        </Header>

答案 2 :(得分:1)

我找到了执行此操作的最佳方法及其对我的工作。

<Header transparent>
    <Left style={{ flex: 1 }}>
        <Icon name='arrow-back' />
    </Left>
    <Body style={{ flex: 1 }}>
        <Title style={{ justifyContent: 'center', color: '#9fabdd' }}>Home</Title>
    </Body>
    <Right style={{ flex: 1 }}>
        <Icon name='arrow-back' />
    </Right>
</Header>

答案 3 :(得分:0)

static navigationOptions = ({ navigation }) => {
    return {

        headerTitle: (

  <Image  style={{width: 50, height: 10}} alignItems='center' source={require('../assets/zazzy.png')} />
  </View>
 ),
        headerLeft: (
            <View style={{ padding: 10 }}>
                <Ionicons name="ios-apps" color='gold' size={24} onPress={() => navigation.navigate('DrawerOpen')} />
            </View>
        ),
         headerRight: (
            <View style={{ padding: 10 }}>
                <Ionicons name="md-search" color='silver'  size={24} onPress={() => navigation.navigate('DrawerOpen')} />
            </View>
        )

    }
}
render() {
    return (
        <HomeScreenTabNavigator screenProps={{ navigation: this.props.navigation }} />

    )
}

}

答案 4 :(得分:0)

You can also try this one:

      <Header>
          <Left style={{ flex: 1 }}>
            <Icon name="arrow-back" />
          </Left>
          <Body style={{ flex: 1 }}>
            <Title style={{ alignSelf: "center" }}>Seminars</Title>
          </Body>
          <Right style={{ flex: 1 }}>
            <Icon name="menu" />
          </Right>
        </Header>

答案 5 :(得分:0)

      <Container style={styles.container}>
            <Header style={styles.header}>
                <Left style={{flex:1}}>
                    <Icon name='arrow-back' />
                </Left>
                <Body style={{flex:1}>
                    <Title style={{width:'100%'}}>Seminars</Title>
                </Body>
                <Right style={{flex:1}}>
                    <Icon name='menu' />
                </Right>
            </Header>
            <Content contentContainerStyle={styles.content} >
                <Text>Content Here</Text>
            </Content>
        </Container>

当左右项目的大小不相等时,我发现这是一种解决方案,适用于android和iOS

答案 6 :(得分:0)

<Header>
  <Left style={{flex: 1}} />
  <Body style={{flex: 1}}>
    <Title style={{alignSelf: 'center'}}>Header</Title>
  </Body>
  <Right style={{flex: 1}} />
</Header>
相关问题