简体   繁体   English

React Native中的Flex标头布局

[英]Flex header layout in React Native

I'm new here on react native, And i was trying to align the back button to the left, But keeping the title on the center, But nothing works, This is the code. 我在这里是本机上的新手,并且我试图将“后退”按钮向左对齐,但是将标题居中,但没有任何效果,这是代码。

The TouchableOpacity is the back button, And the Text is the title, Each one have his own style. TouchableOpacity是后退按钮,Text是标题,每个人都有自己的风格。

 import React, { Component } from 'react'; import { View, Text, TouchableOpacity, Image } from 'react-native'; import { Colors } from '../Variables'; class Header extends Component { render(){ return( <View style={ styles.headerStyle }> <TouchableOpacity> <Image style={ styles.backBtnStyle } source={ require('../graphics/icons/arrow_left_white.png') }/> </TouchableOpacity> <Text style={ styles.titleStyle }> TITLE </Text> </View> ); } } const styles = { headerStyle: { backgroundColor: Colors.primary, flexDirection: 'row', alignItems: 'center', }, backBtnStyle: { width: 25, height: 25, margin: 10, }, titleStyle: { color: '#fff', textAlign: 'center', fontSize: 25, } }; export default Header; 

Thanks for helping. 感谢您的帮助。

You are using alignItems: 'center' in your container, so everything will be in the center. 您在容器中使用alignItems: 'center' ,因此所有内容都将位于中心。

In your case, there are a lot of solutions available. 在您的情况下,有很多解决方案可用。 The more simple is just use a position: absolute; left: 15 更简单的就是使用position: absolute; left: 15 position: absolute; left: 15 in the styles.backBtnStyle . position: absolute; left: 15 styles.backBtnStyle position: absolute; left: 15

You should try to view this document about Flex in React native and practive more to mastered it. 您应该尝试在React本机中查看有关Flex的本文档 ,并且更实际地掌握它。

In Your case, just simplify add alignSelf: 'flex-start' to backBtnStyle StyleSheet to make it in first of your parent component 在您的情况下,只需简化将alignSelf: 'flex-start'到backBtnStyle StyleSheet中,使其在您的父组件的第一个组件中

Here is demo code: 这是演示代码:

headerStyle: {
    backgroundColor: Colors.primary,
    flexDirection: 'row',
    alignItems: 'center',
    alignSelf: 'flex-start'
},

<TouchableOpacity style={styles.backBtnStyle}>
   <Image source={require('../graphics/icons/arrow_left_white.png') }/>
</TouchableOpacity>

You see: TouchableOpacity contain a Viewable Layout and wrap your image then you need to set style for TouchableOpacity to make flex work, not at Image. 您会看到:TouchableOpacity包含一个Viewable Layout并包装您的图像,然后您需要为TouchableOpacity设置样式以使flex工作,而不是在Image处。

From React NativeTouchableOpacity Documents : Opacity is controlled by wrapping the children in an Animated.View, which is added to the view hierarchy. 来自React NativeTouchableOpacity文档 :不透明度是通过将子级包装在Animated.View中控制的,该Animated.View已添加到视图层次结构中。 Be aware that this can affect layout. 请注意,这会影响布局。

You can see Touchable Opacity Document here 您可以在此处看到可触摸的不透明度文档

Note : Set Your StyleSheet on StyleSheet.create() to make it create one and only one time when your bundle load. 注意 :在StyleSheet.create()上设置StyleSheet,以使其在加载包时一次创建一次,并且仅创建一次。 It make your app light weight and faster 它使您的应用重量轻且速度更快

Read about React Native StyleSheet here 在此处阅读有关React Native StyleSheet的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM