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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 虚拟机连不上网所有可能我都遇到

    虚拟机连不上网所有可能我都遇到ping不通,主机访问不了虚拟机等等等等虚拟机ping不通VWnet8的ip、ping不通网关、ping不通百度、yum出现tryothermirror。虚拟机ping不通VWnet8的ip、ping不通网关、ping不通百度、yum出现tryothermirror。首先要编辑虚拟机网络为NAT模式,并设定网关、子网掩码。然后进入NAT设置里,将允许任何组织唯一标识符勾选,这个有时候系统默认没钩。然后进入DHCP设计起始ip和结束ip。进入本地网络,修改VWnet8适配器右键属性

    2022年6月26日
    30
  • JetBrains WebStorm 安装教程

    JetBrains WebStorm 安装教程首先声明,此方法仅用来参考学习,不得用于商业用途,请支持正版,学生可以免费申请到正版软件。网上有很多激活成功教程方法,可能不同的版本不一样,这篇文章就只针对JetBrainsWebStorm2018.1.5×64版本的软件。因为本人用的就是这个版本,亲测有效。——————2019年10月首先需要下载一个jar包:JetbrainsIde…

    2022年6月16日
    45
  • mac如何查看已连接wifi的密码「建议收藏」

    mac如何查看已连接wifi的密码「建议收藏」可以通道mac自带的“钥匙串访问”功能查看。选择需要查询的wifi名称,右击选择“将密码拷贝到剪贴板”,输入管理员密码后,密码就拷贝好了。找个地方粘贴即可看到密码

    2022年8月4日
    11
  • Cpu流水线_cpu流水线预测

    Cpu流水线_cpu流水线预测原文网址:原文英文原文:AJourneyThroughtheCPUPipeline 编译:@deuso_ICT   作为程序员,CPU在我们的工作中扮演了核心角色,因此了解处理器内部的工作方式对程序员来说不无裨益。  CPU是如何工作的呢?一条指令执行需要多长时间?当我们讨论某个新款处理器拥有12级流水线还是18级流水线,甚至是更深的31级流水线时,这到些都意味着什么呢?…

    2022年4月19日
    57
  • eureka集群高可用配置[通俗易懂]

    eureka集群高可用配置[通俗易懂]网上讲这个东西的很多,抄来抄去的,大部分类似,多数没讲明白为什么那么配置。譬如eureka.client.register-with-eureka和fetch-registry是否要配置,配不配区别在哪里;eureka的客户端添加service-url时,是不是需要把所有的eureka的server地址都写上,还是只需要写一个server就可以了(因为server之间已经相互注册了)?如果写上了所…

    2022年6月14日
    55
  • 完美解决方案_onActivityResult

    完美解决方案_onActivityResult在Service中启动Activity,会报错如下:Intentintent=newIntent(MyService.this,Main2Activity.class);startActivity(intent);…

    2022年9月28日
    1

发表回复

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

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