用TabNavigator做一個(gè)符合Material Design的底部導(dǎo)航條

如有轉(zhuǎn)載,請(qǐng)申明:轉(zhuǎn)載自 IT天宇: http://www.itdecent.cn/p/bdad2cd06413

前言

由于工作和學(xué)習(xí)原因,幾個(gè)月沒(méi)寫博客了。
博主在使用 React Native 的 TabNavigator 時(shí),發(fā)現(xiàn)一些小問(wèn)題,查了很久資料也沒(méi)查到,最后自己摸索實(shí)驗(yàn)了好久,總算實(shí)現(xiàn)了。

我想要這樣的效果:


實(shí)際上,默認(rèn)是這樣的:


原因

What? 怎么開(kāi)始寫 React Native 的教程了?

由于工作需要,最近轉(zhuǎn)向?qū)W習(xí) React Native,只能一句話形容 “根本停不下來(lái)”。好吧, app 轉(zhuǎn) java 狗之路走得真曲折。

目錄

1. 引入 TabNavigator

現(xiàn)在官方推薦使用的導(dǎo)航組件是 React Navigation

安裝

npm install --save react-navigation

導(dǎo)入

import {
    TabNavigator,
} from 'react-navigation';

感嘆 ES6 寫法簡(jiǎn)潔而強(qiáng)大。

2. 加上 TabItem

編寫 Item

為了顯示 4 個(gè)平行的界面,我們需要寫 4 個(gè)組件。
這里直接寫 4 個(gè)只含有 Text 的組件。

Home.js

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

export default class Home extends Component {
    render() {
        return (
            <Text>首頁(yè)</Text>
        )
    }
}

Find.js / Message.js / Me.js

和 Home.js 幾乎一樣,只是把 <Text>首頁(yè)</Text> 中的文字改成相應(yīng)的。

使用 TabNavigator

先導(dǎo)入需要的組件,包括上面自己寫的 4 個(gè) Item

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

import {
    TabNavigator,
} from 'react-navigation';

import Home from './Home'
import Find from './Find'
import Message from './Message'
import Me from './Me'

然后寫一個(gè)組件來(lái)使用 TabNavigator

export default class Start extends Component {
    render() {
        const Tabs = TabNavigator(
            {
                Home: {
                    screen: Home,
                    navigationOptions: {
                        tabBarLabel: '首頁(yè)',
                        tabBarIcon: ({tintColor}) => <Image
                            style={[styles.tabBarImage, {tintColor: tintColor}]}
                            source={require('./images/tabbar_home.png')}/>,
                    }
                },

                Find: {
                    screen: Find,
                    navigationOptions: {
                        tabBarLabel: '發(fā)現(xiàn)',
                        tabBarIcon: ({tintColor}) => <Image
                            style={[styles.tabBarImage, {tintColor: tintColor}]}
                            source={require('./images/tabbar_find.png')}
                        />,
                    }
                },

                Message: {
                    screen: Message,
                    navigationOptions: {
                        tabBarLabel: '消息',
                        tabBarIcon: ({tintColor}) => <Image
                            style={[styles.tabBarImage, {tintColor: tintColor}]}
                            source={require('./images/tabbar_message_center.png')}
                        />,
                    }

                },

                Me: {
                    screen: Me,
                    navigationOptions: {
                        tabBarLabel: '我的',
                        tabBarIcon: ({tintColor}) => <Image
                            style={[styles.tabBarImage, {tintColor: tintColor}]}
                            source={require('./images/tabbar_me.png')}
                        />,
                    }
                }
            },
            {
                animationEnabled: true,
                tabBarPosition: 'bottom',
                swipeEnabled: true,
                backBehavior: 'none',
                tabBarOptions: {
                    activeTintColor: 'orange',
                    inactiveTintColor: 'gray',
                    showIcon: true,
                    indicatorStyle: {
                        height: 0,
                    },
                    style: styles.tabBar,
                    tabStyle: styles.tabBarItem,
                    labelStyle: styles.tabBarLabel,
                    pressColor: 'gray',
                    pressOpacity: 0.8,
                    upperCaseLabel: false,
                },

            }
        )

        return (
            <Tabs/>
        )
    }
}

const styles = StyleSheet.create({
    tabBarImage: {
        width: 24,
        height: 24,
    },
    tabBar: {
        backgroundColor: 'white',
    },
    tabBarLabel: {
        fontSize: 12,
    },
    tabBarItem: {
        height: 56,
    },
})

沒(méi)錯(cuò),和你網(wǎng)上看到的教程幾乎一樣,這個(gè)時(shí)候把這個(gè)組件渲染運(yùn)行的話,就是上面看到的默認(rèn)的情況。

3. 修改樣式達(dá)到 Material Design 要求

上面顯然不是想要的效果,MD規(guī)范為:整體高度 56dp,未選中的時(shí)候,圖標(biāo) 24dp,距離頂部 8dp;文字距離底部 8dp,距離圖標(biāo) 6dp。


為了達(dá)到 MD 的規(guī)范,我們需要修改一下。

tabBarOptions 屬性中加入圖標(biāo)的樣式

iconStyle: styles.tabBarIcon,

然后在樣式中做如下修改

tabBarImage: {
    width: 24,
    height: 24,
    marginTop: 8,
},
tabBarIcon: {
    height: 32,
},

tabBarLabel: {
    fontSize: 12,
    marginBottom: 8,
    marginTop: 6,
},

tabBarImage 中增加了 marginTop
增加樣式 tabBarIcon
tabBarLabel 中增加了 marginTop, marginBottom

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,983評(píng)論 25 709
  • 五年前開(kāi)始學(xué)古琴,斷斷續(xù)續(xù)上了兩年課,每周六上一小時(shí)課,從右手指法開(kāi)始練起。開(kāi)始學(xué)琴的艱辛還歷歷在目。學(xué)琴的初...
    閑度閱讀 1,474評(píng)論 2 2
  • IE(Industrial Engineering) 工業(yè)工程。 經(jīng)常使用的有5s,ECRS,PDCA,SWOT,...
    范張雞黍閱讀 347評(píng)論 0 0
  • 我有一個(gè)手機(jī),手機(jī)里有一張電話卡,手機(jī)的第一張卡是它,而它之前卻換過(guò)不少手機(jī)。 它是一張不甘寂寞的電話卡,它換了...
    撒謊大王閱讀 272評(píng)論 0 0

友情鏈接更多精彩內(nèi)容