美文网首页React native
react-native--05界面布局

react-native--05界面布局

作者: 极光火狐狸 | 来源:发表于2017-01-12 00:09 被阅读527次

在html中所有的布局都依赖于css样式中的style继承(css中确切来说应该是不存在继承关系的,而是通过样式名称直接引用),css拥有非常丰富的特性。
在react native中,样式的配饰和html有非常多的相似,比如说在html中声明背景颜色采用 background 或者 background-color,但是在react native中采用 backgroundColor, 所有的样式声明都是采用这种峰驼式风格

View(视图)

react native中的View组件类似与html中的div,下面是一些针对View布局的一些测试代码。一个组件类中只能存在一个一级View。
正确的代码

import React, { Component } from 'react';
import { AppRegistry, View, Text } from 'react-native';

class FlexDimensionsBasics extends Component {
    render() {
        return (
            <View><Text>hello world!</Text></View>
        );
    }
};

AppRegistry.registerComponent('rn_practice', () => FlexDimensionsBasics);

错误的代码

import React, { Component } from 'react';
import { AppRegistry, View, Text } from 'react-native';

class FlexDimensionsBasics extends Component {
    render() {
        return (
            <View><Text>hello world!</Text></View>
            <View><Text>hello world!</Text></View>
        );
    }
};

AppRegistry.registerComponent('rn_practice', () => FlexDimensionsBasics);
multiView(多个视图)

一个组件类中只能存在一个一级View,因此多个View只能包裹在一级View中。

import React, { Component } from 'react';
import { AppRegistry, View, Text } from 'react-native';

class FlexDimensionsBasics extends Component {
    render() {
        return (
            <View>
                <View><Text>hello world!</Text></View>
                <View><Text>hello world!</Text></View>
                <View><Text>hello world!</Text></View>
            </View>

        );
    }
};

AppRegistry.registerComponent('rn_practice', () => FlexDimensionsBasics);   
View Layout(视图布局)

布局涉及到的就是 多个同级元素保持同一行(inline)自适应宽内间距外间距横向居中竖向居中向左居中向右剧中固定位置、,另外react native还提供了一种==自适应高==。
所有布局的详细参数,请参考react native官网提供的说明文档

  1. 宽和高 引用官方源码

    import React, { Component } from 'react';
    import { AppRegistry, View } from 'react-native';
    
    class FixedDimensionsBasics extends Component {
        render() {
            return (
                <View>
                    <View style={{width: 50, height: 50, backgroundColor: 'powderblue'}} />
                    <View style={{width: 100, height: 100, backgroundColor: 'skyblue'}} />
                    <View style={{width: 150, height: 150, backgroundColor: 'steelblue'}} />
                </View>
            );
        }
    };
    
    AppRegistry.registerComponent('rn_practice', () => FixedDimensionsBasics);
    
    宽和高
  2. 多个同级元素保持同一行(inline) 引用官网源码

     import React, { Component } from 'react';
     import { AppRegistry, View } from 'react-native';
     
     class FlexDirectionBasics extends Component {
         render() {
             return (
                 <View style={{flex: 1, flexDirection: 'row'}}>
                     <View style={{width: 50, height: 50, backgroundColor: 'powderblue'}} />
                     <View style={{width: 100, height: 100, backgroundColor: 'skyblue'}} />
                     <View style={{width: 150, height: 150, backgroundColor: 'steelblue'}} />
                 </View>
             );
         }
     };
     
     AppRegistry.registerComponent('rn_practice', () => FlexDirectionBasics);  
    
    多个同级元素保持同一行(inline)
  3. 自适应宽 引用官网源码

     import React, { Component } from 'react';
     import { AppRegistry, View } from 'react-native';
     
     class FlexDimensionsBasics extends Component {
         render() {
             return (
                 <View style={{flex: 1, flexDirection: 'row'}}>
                     <View style={{flex: 1, backgroundColor: 'powderblue'}} />
                     <View style={{flex: 2, backgroundColor: 'skyblue'}} />
                     <View style={{flex: 3, backgroundColor: 'steelblue'}} />
                 </View>
             );
         }
     };
     
     AppRegistry.registerComponent('rn_practice', () => FlexDimensionsBasics);   
    
    自适应宽
  4. 自适应高 引用官网源码

     import React, { Component } from 'react';
     import { AppRegistry, View } from 'react-native';
     
     class FlexDimensionsBasics extends Component {
         render() {
             return (
                 <View style={{flex: 1}}>
                     <View style={{flex: 1, backgroundColor: 'powderblue'}} />
                     <View style={{flex: 2, backgroundColor: 'skyblue'}} />
                     <View style={{flex: 3, backgroundColor: 'steelblue'}} />
                 </View>
             );
         }
     };
     
     AppRegistry.registerComponent('rn_practice', () => FlexDimensionsBasics);   
    
    自适应高
  5. 内间距(padding)

     import React, { Component } from 'react';
     import { AppRegistry, View, Text } from 'react-native';
     
     class FlexDimensionsBasics extends Component {
         render() {
             return (
                 <View style={{flexDirection: 'row'}}>
                     <Text style={{paddingLeft: 100, paddingTop: 200}}>Hello World!</Text>
                 </View>
             );
         }
     };
     
     AppRegistry.registerComponent('rn_practice', () => FlexDimensionsBasics);   
    
    内间距(padding)
  6. 外间距(margin)

     import React, { Component } from 'react';
     import { AppRegistry, View, Text } from 'react-native';
     
     class FlexDimensionsBasics extends Component {
         render() {
             return (
                 <View style={{flex: 1}}>
                     <View style={{margin: 100, width: 200, height: 200, backgroundColor: 'white'}}>
                         <Text>Box 1</Text>
                         <View style={{margin: 30, backgroundColor: 'powderblue'}}>
                             <Text>Box 2</Text>
                         </View>
                     </View>
                 </View>
             );
         }
     };
     
     AppRegistry.registerComponent('rn_practice', () => FlexDimensionsBasics);      
    
    外间距(margin)
  1. 对齐(align)

    采用html的方式来实现向右对齐

     import React, { Component } from 'react';
     import { AppRegistry, View } from 'react-native';
     
     class JustifyContentBasics extends Component {
         render() {
             return (
                 <View>
                     <View style={{width: 50, height: 50, backgroundColor: 'powderblue', position: 'absolute', right: 0}} />
                 </View>
             );
         }
     };
     
     AppRegistry.registerComponent('rn_practice', () => JustifyContentBasics);   
    

    采用flexDirection实现
    row-reverse其实并非一种常规的向右对齐,而是反向对齐。

     import React, { Component } from 'react';
     import { AppRegistry, View } from 'react-native';
     
     class JustifyContentBasics extends Component {
         render() {
             return (
                 <View style={{flexDirection: 'row-reverse'}}>
                     <View style={{width: 50, height: 50, backgroundColor: 'powderblue'}} />
                 </View>
             );
         }
     };
     
     AppRegistry.registerComponent('rn_practice', () => JustifyContentBasics);   
    

    采用flexDirection+justifyContent

     import React, { Component } from 'react';
     import { AppRegistry, View } from 'react-native';
     
     class JustifyContentBasics extends Component {
         render() {
             return (
                 <View style={{flexDirection: 'row', justifyContent: 'flex-end'}}>
                     <View style={{width: 50, height: 50, backgroundColor: 'powderblue'}} />
                 </View>
             );
         }
     };
     
     AppRegistry.registerComponent('rn_practice', () => JustifyContentBasics);   
    
    向右对齐

    上述三种都是向右对齐的例子,更多、更灵活的方法,请参考mozilla公司定义的flex-Directionjustify-Content标准。

    mozilla justifyContent
  1. 固定位置

     import React, { Component } from 'react';
     import { AppRegistry, View } from 'react-native';
     
     class JustifyContentBasics extends Component {
         render() {
             return (
                 <View style={{flex: 1}}>
                     <View style={{width: 50, height: 50, backgroundColor: 'powderblue', 
                     position: 'absolute', right: 100, bottom: 100}} />
                 </View>
             );
         }
     };
     
     AppRegistry.registerComponent('rn_practice', () => JustifyContentBasics);   
    
    固定位置
  2. 文本竖向居中

     import React, { Component } from 'react';
     import {View, AppRegistry, Text} from 'react-native';
     
     class Bananaes extends Component {
         render() {
             return (
                 <View style={{height: 80, margin: 130, width: 80}}>
                     <View style={{flex: 1, backgroundColor: "teal", justifyContent: 'center'}}>
                         <Text>Hello</Text>
                     </View>
                 </View>
             )
         }
     }
     
     AppRegistry.registerComponent('rn_practice', () => Bananaes);   
    
    文本竖向居中文本竖向居中
  3. 文本横向居中

    import React, { Component } from 'react';
    import {View, AppRegistry, Text} from 'react-native';
    
    class Bananaes extends Component {
        render() {
            return (
                <View style={{height: 80, margin: 130, width: 80}}>
                    <View style={{flex: 1, backgroundColor: "teal", alignItems: 'center'}}>
                        <Text>Hello</Text>
                    </View>
                </View>
            )
        }
    }
    
    AppRegistry.registerComponent('rn_practice', () => Bananaes);
    
    文本横向居中文本横向居中
  4. 文本横向竖向居中

    import React, { Component } from 'react';
    import {View, AppRegistry, Text} from 'react-native';
    
    class Bananaes extends Component {
        render() {
            return (
                <View style={{height: 80, margin: 130, width: 80}}>
                    <View style={{flex: 1, backgroundColor: "teal", alignItems: 'center', justifyContent: 'center'}}>
                        <Text>Hello</Text>
                    </View>
                </View>
            )
        }
    }
    
    AppRegistry.registerComponent('rn_practice', () => Bananaes);
    
    文本横向居中文本横向居中

相关文章

  • react-native--05界面布局

    在html中所有的布局都依赖于css样式中的style继承(css中确切来说应该是不存在继承关系的,而是通过样式名...

  • ListView的用法

    -主界面布局: -adapter布局: -adapter,java写法: -主界面写法: -adapter,kot...

  • RecyclerView的用法

    -主界面布局: -adapter布局: -adapter,java写法 -主界面代码: -adapter,kotl...

  • 界面布局

    UIViewController的界面布局属性: 1、导航栏透明translucent属性默认为YES,意味着子控...

  • Xcode快捷键和界面布局认识

    按键与符号 Xcode界面布局 Xcode整体布局 Bar区域 Toolbar界面与功能 Navigator区域 ...

  • 如何选择UI 界面布局样式?来看丁香园设计师的总结!

    什么是界面布局样式? 界面布局样式是指用于区分信息内容,层次的版式设计的具体方式。 总结和了解目前常用的界面布局样...

  • 2020-10-06

    Android常见界面布局:RelativeLayout(相对布局) LinearLayout(线性...

  • 第二章 Android常见界面布局

    在XML文件中编写布局 界面布局编写方式在xml文件中编写布局:有效的将界面中布局的代码和Java代码隔离,使程序...

  • 无标题文章

    qq界面: qq布局:

  • 入门

    一、界面布局介绍 RelativeLayout(相对布局)和LinearLayout(线性布局)android:l...

网友评论

    本文标题:react-native--05界面布局

    本文链接:https://www.haomeiwen.com/subject/cccdbttx.html