前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >RN实现头部NavBar

RN实现头部NavBar

作者头像
提莫队长
发布2018-05-18 15:18:30
1.8K0
发布2018-05-18 15:18:30
举报
文章被收录于专栏:刘晓杰刘晓杰

(1)实现NavBar

代码语言:javascript
复制
import React, {Component, PropTypes} from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    Dimensions,
    Platform
} from 'react-native';

const {width, height} = Dimensions.get('window');

export default class GDNavBar extends Component {

    static propTypes = {
        leftItem: PropTypes.func,
        titleItem: PropTypes.func,
        rightItem: PropTypes.func,
    };

    renderLeftItem() {
        if (this.props.leftItem === undefined) {
            return;
        }
        return this.props.leftItem();
    };

    renderTitleItem() {
        if (this.props.titleItem === undefined) {
            return;
        }
        return this.props.titleItem();
    };

    renderRightItem() {
        if (this.props.rightItem === undefined) {
            return;
        }
        return this.props.rightItem();
    };

    render() {
        return (
            <View style={styles.container}>
                <View>
                    {this.renderLeftItem()}
                </View>

                <View>
                    {this.renderTitleItem()}
                </View>

                <View>
                    {this.renderRightItem()}
                </View>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        width: width,
        height: Platform.OS === 'ios' ? 64 : 44,
        backgroundColor: 'red',
        flexDirection: 'row',//按行排列
        justifyContent: 'space-between',
        alignItems: 'center',
        borderBottomWidth: 0.5,
        borderBottomColor: 'gray',
        paddingTop: Platform.OS === 'ios' ? 15 : 0,
    },
});

(2)调用

代码语言:javascript
复制
import React, {Component} from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    TouchableOpacity,
    Image,
    ListView,
    Dimensions
} from 'react-native';

import NavBar from '../main/GDNavBar';
import Cell from '../main/GDHotCell';

const {width, height} = Dimensions.get('window');

export default class GDHalfHourHot extends Component {

    constructor(props) {
        super(props);
    }

    renderTitleItem() {
        return (
            <Text style={styles.titleItem}>近半小时热门</Text>
        );
    };

    renderRightItem() {
        return (
            <TouchableOpacity>
                <Text style={styles.rightItem}>关闭</Text>
            </TouchableOpacity>
        );
    };

    render() {
        return (
        //这里left默认没有
            <View style={styles.container}>
                <NavBar
                    titleItem={() => this.renderTitleItem()}
                    rightItem={() => this.renderRightItem()}
                />
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        alignItems: 'center',
        backgroundColor: 'white',
    },
    titleItem: {
        fontSize: 17,
        color: 'black',
        marginLeft: 50,
    },
    rightItem: {
        fontSize: 17,
        color: 'rgba(123,178,114,1.0)',
        marginRight: 15,
    },
});
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年08月27日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • (1)实现NavBar
  • (2)调用
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档