search
HomeWeb Front-endHTML TutorialReact Native 0.23 iOS: 体验灵活的flexbox布局UI_html/css_WEB-ITnose

React Native使用CSS3中的 flexbox 做布局,比如我们实现这样一个简单布局界面。

如下图(纵向):

横向:

布局逻辑是这样的:

  • 3行,第一行高度固定50pt,第二行占剩余空间的四分之三,第三行占剩余空间的四分之一。
  • 在第二行中,又包含3列元素,每一列元素各占三分之一的空间,并且根据内容水平居中。在垂直排版上,第一列向下对其,第二列居中,第三列拉伸。

Component代码:

class FlexboxLayout extends Component {    render() {        return (            <View style={styles.container}>                <View style={styles.row1}></View>                <View style={styles.row2}>                    <Text style={styles.cell1}>1</Text>                    <Text style={styles.cell2}>2</Text>                    <Text style={styles.cell3}>3</Text>                </View>                <View style={styles.row3}></View>            </View>        );    }}

主要工作在Style设置上,一些实现上需要注意的地方,首先3行的实现上,如果是固定高度,只需要设置 height 即可,值是数字,代表iOS中的点,如果是比例,则需要设置 flex 值,按照上面的样子,第二行占四分之三, flex 值是3,第三行占四分之一, flex 值是1.

接着是第二行中三列View的实现,React Native中 flexDirection 默认是 column 即纵向排列,所以这里需要手动设置 flexDirection 为 row ,这样才会横向排列。然后三列会横向居中,这个要设置 justifyContent 为 space-around , justifyContent 实际上对应CSS3中flexbox里的 justify-content 属性,具体的flexbox属性以及其效果建议看参考 CSS-Tricks 。

样式代码:

const styles = StyleSheet.create({    container: {        flex: 1,    },    row1: {        height: 50,        backgroundColor: '#09A9DE'    },    row2: {        flex: 3,        backgroundColor: '#09DE53',        flexDirection: 'row',        justifyContent: 'space-around',        alignItems: 'center'    },    row3: {        flex: 1,        backgroundColor: '#DEDE09'    },    cell1: {        backgroundColor: '#BDBDBD',        padding: 20,        alignSelf: 'flex-end'    },    cell2: {        backgroundColor: '#BDBDBD',        padding: 20    },    cell3: {        backgroundColor: '#BDBDBD',        padding: 20,        alignSelf: 'stretch'    }});

最后对于每列纵向的对其,可以设置父View的 alignItems 属性,可以把这个值当做排列时的默认值,我们这里设置为 center ,这样三列都会默认纵向居中,而上图中的实现,第一列和第三列分别是下对其和拉伸,和默认值是不一样的,这样的话,可以通过手动设置子View的 alignSelf 属性,所以第一列的 alignSelf 值是 flex-end ,第三列的 alignSelf 值是 stretch .

最后,个人觉得,暂且不去比较整个React Native和原生Native(这个话题太大),只去比较这个flexbox布局和目前iOS开发的Autolayout布局,前者可以说是更优于后者的。事实上,在实际使用Autolayout中确实会遇到很多问题,比如一些比较复杂的页面在Xcode Interface Builder中编辑或者去阅读其他人的Autolayout约束会显得比较困难,再比如在代码中使用Autolayout时,不同团队可能会选择不同的Autolayout代码框架,这也会造成一定麻烦。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is the purpose of the <datalist> element?What is the purpose of the <datalist> element?Mar 21, 2025 pm 12:33 PM

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <progress> element?What is the purpose of the <progress> element?Mar 21, 2025 pm 12:34 PM

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

What is the purpose of the <meter> element?What is the purpose of the <meter> element?Mar 21, 2025 pm 12:35 PM

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

What is the viewport meta tag? Why is it important for responsive design?What is the viewport meta tag? Why is it important for responsive design?Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

What is the purpose of the <iframe> tag? What are the security considerations when using it?What is the purpose of the <iframe> tag? What are the security considerations when using it?Mar 20, 2025 pm 06:05 PM

The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

How do I use the HTML5 <time> element to represent dates and times semantically?How do I use the HTML5 <time> element to represent dates and times semantically?Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

How do I use HTML5 form validation attributes to validate user input?How do I use HTML5 form validation attributes to validate user input?Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

What are the best practices for cross-browser compatibility in HTML5?What are the best practices for cross-browser compatibility in HTML5?Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),