【React Native 安卓开发】----(View实战之仿携程)【第三篇】

这里讲一下RN中的View组件:
先看一下效果
这里写图片描述

下面是代码,可以直接copy:

后面会给大家一步一步讲解过程,与大家共同成长

import React, { Component } from 'react';
 import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  TextInput,
  ScrollView,
  ListView,
  PixelRatio
} from 'react-native';

class hello extends Component{
  render(){
    return(
      <View style = {styles.flex}>
        <View style = {styles.container}>
            <View style = {[styles.item,styles.center]}>
              <Text style={styles.font}>酒店</Text>
            </View>
            <View style = {[styles.item,styles.lineLeft]}>
             
              <View style = {[styles.item_inner,styles.center,styles.lineBottom]}>
                <Text style={styles.font}>机票</Text>
              </View>
              <View style = {[styles.item_inner,styles.center]}>
                <Text style={styles.font}>火车票</Text>
              </View>

            </View>
            <View style = {[styles.item,styles.lineLeft]}>
              <View style = {[styles.item_inner,styles.center,styles.lineBottom]}>
                <Text style={styles.font}>旅游</Text>
              </View>
              <View style = {[styles.item_inner,styles.center]}>
                <Text style={styles.font}>攻略</Text>
              </View>
            </View>
        </View>
      </View>
    );
  }
};

const styles = StyleSheet.create({
  container:{
    marginTop:200,
    marginLeft:5,
    marginRight:5,
    flexDirection:'row',
    padding:2,
    borderRadius:5,
    height:84,
    backgroundColor : '#ff0067'
  },
  item:{
    flex:1,
    height:80,
  },
  item_inner:{
    flex:1,
    height:40,
    
  },
  center:{
    justifyContent:'center',
    alignItems:'center'
  },
  flex:{
    flex :1
  },
  font:{
    color:'#ffffff',
    fontSize:16,
    fontWeight:'bold'
  },
  lineBottom:{
    borderBottomWidth:1/PixelRatio.get(),
    borderColor:'#ffffff'
  },
  lineLeft:{
    borderLeftWidth:1/PixelRatio.get(),
    borderColor:'#ffffff'
  }
});
AppRegistry.registerComponent('hello', () => hello);

下面让我们一步一步来分解:
#第一步
如图:我们可以想到先做三个View 这三个View使用FlexBox平分,flex都为1,这里的flex其实就相当于安卓里面的weight权重的概念。

这里写图片描述

class hello extends Component{
  render(){
    return(
      <View style = {{backgroundColor : '#ffffff'}}>
        <View style = {{backgroundColor : '#ff0067',height:80,flexDirection:'row'}}>
            <View style = {{flex:1,backgroundColor : '#ff00ff'}}>  
            </View>

            <View style = {{flex:1,backgroundColor : '#ffff00'}}>  
            </View>

            <View style = {{flex:1,backgroundColor : '#00ffff'}}>  
            </View>
        </View>
      </View>
    );
  }
};

#第二步
我们可以考虑到后面两个View里面也是flexBox,flexDirection为默认column.
这里写图片描述

class hello extends Component{
  render(){
    return(
      <View style = {{backgroundColor : '#ffffff'}}>
        <View style = {{backgroundColor : '#ff0067',height:80,flexDirection:'row'}}>
            <View style = {{flex:1,backgroundColor : '#ff00ff'}}>  
            </View>

            <View style = {{flex:1,backgroundColor : '#ffff00'}}>  
              <View style = {{flex:1,backgroundColor : '#0f11ff'}}>  
              </View>
              <View style = {{flex:1,backgroundColor : '#02f3ff'}}>  
              </View>
            </View>

            <View style = {{flex:1,backgroundColor : '#00ffff'}}>  
              <View style = {{flex:1,backgroundColor : '#ff110f'}}>  
              </View>
              <View style = {{flex:1,backgroundColor : '#f2f300'}}>  
              </View>
            </View>
        </View>
      </View>
    );
  }
};

#第三步
添加文字进去
这里写图片描述

class hello extends Component{
  render(){
    return(
      <View style = {{backgroundColor : '#ffffff'}}>
        <View style = {{backgroundColor : '#ff0067',height:80,flexDirection:'row'}}>
            <View style = {{flex:1,backgroundColor : '#ff00ff',justifyContent:'center',alignItems:'center'}}>  
              <Text style ={{color:'#ffffff',fontSize:16,fontWeight:'bold'}}>11111</Text>
            </View>

            <View style = {{flex:1,backgroundColor : '#ffff00'}}>  
              <View style = {{flex:1,backgroundColor : '#0f11ff',justifyContent:'center',alignItems:'center'}}> 
                <Text style ={{color:'#ffffff',fontSize:16,fontWeight:'bold'}}>22222</Text> 
              </View>
              <View style = {{flex:1,backgroundColor : '#02f3ff',justifyContent:'center',alignItems:'center',justifyContent:'center',alignItems:'center'}}>  
                <Text style ={{color:'#ffffff',fontSize:16,fontWeight:'bold'}}>33333</Text> 
              </View>
            </View>

            <View style = {{flex:1,backgroundColor : '#00ffff'}}>  
              <View style = {{flex:1,backgroundColor : '#ff110f',justifyContent:'center',alignItems:'center'}}>  
                <Text style ={{color:'#ffffff',fontSize:16,fontWeight:'bold'}}>44444</Text> 
              </View>
              <View style = {{flex:1,backgroundColor : '#f2f300',justifyContent:'center',alignItems:'center'}}>  
                <Text style ={{color:'#ffffff',fontSize:16,fontWeight:'bold'}}>55555</Text> 
              </View>
            </View>
        </View>
      </View>
    );
  }
};

现在代码看起来有点乱 不过基本效果有了 ,接下来我们把style提取出来,然后再加上一些处理就可以得到我们文章开头看到的效果了。是不是很酷炫。

这里在样式中用到了PixelRatio。
这个就是获取屏幕的设备像素比。window.devicePixelRatio是设备上物理像素和设备独立像素(device-independent pixels (dips))的比例。
公式表示就是:window.devicePixelRatio = 物理像素 / dips

dip或dp,(device independent pixels,设备独立像素)与屏幕密度有关。dip可以用来辅助区分视网膜设备还是非视网膜设备。

说道dp,安卓同学都应该很熟悉才对,这里就不过多介绍了。

扫码关注公众号“伟大程序猿的诞生“,更多干货新鲜文章等着你~

公众号回复“资料获取”,获取更多干货哦~

有问题添加本人微信号“fenghuokeji996” 或扫描博客导航栏本人二维码

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值