React Native(四)——顶部以及底部导航栏实现方式

React Native(四)——顶部以及底部导航栏实现方式

大家好,又见面了,我是全栈君。

效果图:

2

一步一步慢慢来:

其实刚入手做app的时候,就应该做出简单的顶部以及底部导航栏。无奈又在忙其他事情,导致这些现在才整理出来。

u=1201501237,3797835182&fm=27&gp=0

1.顶部导航栏:react-native-scrollable-tab-view;文档地址:https://github.com/skv-headless/react-native-scrollable-tab-view

2.底部导航栏:react-navigation中的TabNavigator;文档地址:https://reactnavigation.org/docs/navigators/tab


3.一直想让index.android.js的代码简洁一些,苦思不得其解,直到现在才找到了一点“路径”,看这版的源代码:

index.android.js:

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

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

//顶部导航栏
import TopTabBar from './Views/TopTabBar';
//底部导航栏
import BottomTabBar from './Views/BottomTabBar';


export default class ywg extends Component {
  render() {
    return (
      <View style={ 
     {flex:1}}>
        <TopTabBar/>
        <BottomTabBar/>
      </View>    
    );
  }
}

AppRegistry.registerComponent('ywg', () => ywg);

bee0518dd21ff696b703bce9cd15c10c

怎样?够简单吧……对了,这样的代码看起来才比较“优雅”(容忍zheng小叶正儿八经的胡说八道哦~)而主要的代码就在

//顶部导航栏
import TopTabBar from './Views/TopTabBar';
//底部导航栏
import BottomTabBar from './Views/BottomTabBar';

这两个红色的文件中。

【重点注意】将两个Component同时使用的时候,一定要在最外层的View上定义样式,否则任你怎样摆弄,它们总是不会展现“庐山真面目”,具体的文档在:http://reactnative.cn/docs/0.46/layout-props.html

QQ截图20170928135412

这是项目文件路径。

BottomTabBar.js:

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

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

//底部导航栏
import { TabNavigator } from "react-navigation";

class Home extends React.Component {
  static navigationOptions = {
      tabBarLabel: '热点',
      tabBarIcon: ({ focused, tintColor }) => (
          <Image
              source={focused ? require('../Images/hot_hover.png') : require('../Images/hot.png')}
              style={
   
   { width: 26, height: 26, tintColor: tintColor }}
          />
      )
  };
  render() {
      return (
          <View style={styles.container}>
              <Text>这是热点</Text>
          </View>
      );
  }
}   class Circle extends React.Component {
  static navigationOptions = {
      tabBarLabel: '圈子',
      tabBarIcon: ({ focused, tintColor }) => (
          <Image
              source={focused ? require('../Images/coterie_hover.png') : require('../Images/coterie.png')}
              style={
   
   { width: 26, height: 26, tintColor: tintColor }}
          />
      )
  };
  render() {
      return (
          <View style={styles.container}>
              <Text>这是圈子内容</Text>
          </View>
      );
  }
}   class Tools extends React.Component {
  static navigationOptions = {
      tabBarLabel: '工具',
      tabBarIcon: ({ focused, tintColor }) => (
          <Image
              source={focused ? require('../Images/tool.png') : require('../Images/tool.png')}
              style={
   
   { width: 26, height: 26, tintColor: tintColor }}
          />
      )
  };
  render() {
      return (
          <View style={styles.container}>
              <Text>这是工具内容</Text>
          </View>
      );
  }
}
class Mypage extends React.Component {
    static navigationOptions = {
      tabBarLabel: '我的',
      tabBarIcon: ({ focused, tintColor }) => (
        <Image
          source={focused ? require('../Images/my_hover.png') : require('../Images/my.png')}
          style={
   
   { width: 26, height: 26, tintColor: tintColor }}
        />
      )
    };
    render() {
      return (
        <View style={styles.container}>
          <Text>这是我的内容</Text>
        </View>
      );
    }
}

const BottomTabBar = TabNavigator(
  {
    Home: {
      screen: Home,
    },
    Circle: {
      screen: Circle,
    },
    Tools: {
      screen: Tools,
    },
    Mypage: {
      screen: Mypage,
    },
  },
  {
    tabBarOptions: {
      activeTintColor: '#4BC1D2',
      inactiveTintColor: '#000',
      showIcon: true,
      showLabel: true,
      upperCaseLabel: false,
      pressColor: '#823453',
      pressOpacity: 0.8,
      style: {
        backgroundColor: '#fff',
        paddingBottom: 0,
        borderTopWidth: 0.5,
        borderTopColor: '#ccc',
      },
      labelStyle: {
        fontSize: 12,
        margin: 1
      },
      indicatorStyle: { height: 0 }, //android 中TabBar下面会显示一条线,高度设为 0 后就不显示线了
    },
    tabBarPosition: 'bottom',
    swipeEnabled: false,
    animationEnabled: false,
    lazy: true,
    backBehavior: 'none',
  });

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#fff',
  }
});

module.exports = BottomTabBar;

TopTabBar.js:

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  Image
} from 'react-native';
import HomePage from '../Views/HomePage';
import PricePage from '../Views/PricePage';
import InventoryPage from '../Views/InventoryPage';

//顶部导航
var ScrollableTabView = require('react-native-scrollable-tab-view');


export default class TopTabBar extends Component {
  render() {
    return (
      <ScrollableTabView 
       tabBarUnderlineStyle={
   
   {backgroundColor:'#fff'}}
      >
        <HomePage tabLabel="首页" />
        <PricePage tabLabel="成交价" />
        <InventoryPage tabLabel="库存" />
      </ScrollableTabView>
    );
  }
}
module.exports = TopTabBar;

而关于这些的详细介绍可以参考这里(老大的小结):http://www.cnblogs.com/vipstone/p/7516115.html?utm_source=tuicool&utm_medium=referral

美中不足:

怎样才能实现顶部栏、底部栏控制各自部分功能呢?留下来的~~~

faedab64034f78f0f326463270310a55b2191cc5


PS:尴尬的事情猝不及防的发生了……

一直想不明白,顶部导航栏跟底部导航栏同时存在的情况下,怎样控制各自的功能呢?于是再请教完做手机开发的同事后才恍然大悟,原来自己想的顶部导航栏根本不是顶部导航栏,简言之就是自己把布局搞错了!明明只是有底部导航栏,而所谓的“顶部导航栏”也只是底部导航栏中的第一小部分里面嵌套着一个轮播组件,才会给人以错觉,啊啊啊……事实真相居然是这样的~

timg

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/108133.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • errno.h是什么头文件(unistd.h是什么头文件)

    注意:只有当一个库函数失败时,errno才会被设置。当函数成功运行时,errno的值不会被修改。这意味着我们不能通过测试errno的值来判断是否有错误存在。反之,只有当被调用的函数提示有错误发生时检查errno的值才有意义。查看错误代码errno是调试程序的一个重要方法。当linuxCapi函数发生异常时,一般会将errno变量(需includeerrno.h)赋一个整数值,不同的值表示不

    2022年4月16日
    48
  • 全局平均池化(Global Average Pooling)

    出处:LinM,ChenQ,YanS.Networkinnetwork[J].arXivpreprintarXiv:1312.4400,2013.定义:将特征图所有像素值相加求平局,得到一个数值,即用该数值表示对应特征图。目的:替代全连接层效果:减少参数数量,减少计算量,减少过拟合思路:如下图所示。假设最终分成10类,则最后卷积层应该包含10个滤波器(即输…

    2022年4月6日
    64
  • 小型酒店管理系统毕业论文_简易酒店管理系统

    小型酒店管理系统毕业论文_简易酒店管理系统一需求:编写程序模拟酒店管理系统:预订和退订以及查看所有房间1需要有一个酒店类2需要有一个房间类3需要有一个客户端类publicclassTest{}二分析:客户端:1先打印所有房间2等待用户输入,根据输入情况判断是预订还是退订3等待用户输入房间号4调用酒店的预订/退订方法把房间号传入完成预订/退订功能…

    2022年9月25日
    3
  • 知识图谱—知识推理综述(三)

    知识图谱—知识推理综述(三)知识图谱—知识推理综述(三)接上一篇文章知识图谱—知识推理综述(二)3基于表示的知识推理3.1方法简述在之前所介绍的知识推理中,都显示的定义了知识推理所需要的规则,条件等等离散符号。而在基于表示的知识推理中,我们第一步是将知识图谱中的节点和关系进行连续向量空间的映射,需要将其物理表示映射为数值表示,然后在利用数学中的相关算法,通过数值计算的方式进行知识推理。对于映射的向量空间而言,其可以是一个或者多个的向量或者矩阵。基于表示的推理的核心在于“如何表示”,在表示学习的过程中,我们需要的是让算法自

    2022年5月12日
    41
  • 陶瓷电容分类_陶瓷电容电压级别分类

    陶瓷电容分类_陶瓷电容电压级别分类陶瓷电容分为贴装陶瓷电容和插装陶瓷电容两大类。贴装陶瓷电容是多层陶瓷电容,一般耐压不会超过50V,而层数可以达到4000层,插装的陶瓷电容中包括圆片陶瓷电容和独石电容。圆片陶瓷电容也称瓷片电容,是单层的,耐压从50V~6000V都较为普遍。同样容值下,瓷片电容受温度影响更小,更适合应用在微弱信号放大电路的滤波中。独石电容全称是片式多层陶瓷电容器,(网上有资料说其实就是陶瓷贴片电容的

    2022年8月22日
    5
  • [Unity面试] 2022年Unity面试题分享「建议收藏」

    [Unity面试] 2022年Unity面试题分享「建议收藏」2022Unity面试题分享建议收藏

    2022年10月5日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号