react native學習筆記5——布局實戰篇

目標

基礎知識的學習只有在實踐中使用才更容易被理解與吸收,前面幾節都是在介紹基本的屬性概念,我自己是一個比較健忘的人,我估計很多人也跟我一樣在學了基本概念后過不了多久就忘了,或者只是有個印象卻不知道該怎么用該在何處運用這些特性。
本節我們將運用前面所介紹的基本概念,進行布局實戰,實現如下效果:


在此之前先介紹一下幾個常見的網格布局。

網格布局

網格布局示例的完整代碼在:
https://github.com/mronion0603/ReactNativeExercise/blob/master/src/02_flex/GridDemo.js

基本的均分網格布局

最簡單的網格布局,就是平均分布,在容器里面平均分配空間。
將flex都設為1,或各個子元素flex都設置一個相同的值。

代碼示例如下:

<View style={ {flexDirection:'row',height:40}}>
    <View style={ {flex:1,backgroundColor:"grey"}}></View>
    <View style={ {flex:1,backgroundColor:"white"}}></View>
    <View style={ {flex:1,backgroundColor:"grey"}}></View>
</View> 

兩邊固定中間填充的網格

兩邊的元素設置固定寬度width:100,中間的元素設置flex:1

一般標題欄可以通過這種方法布局。
代碼實現如下:

<View style={ {flexDirection:'row',height:40}}>
    <View style={ {width:100,backgroundColor:"grey"}}></View>
    <View style={ {flex:1,backgroundColor:"white"}}></View>
    <View style={ {width:100,backgroundColor:"grey"}}></View>
</View>

嵌套的網格

要實現一些稍微復雜點的效果,通常一層網格是搞不定的,這時需要網格嵌套網格,一層嵌套一層。如下圖所示:

代碼實現:

<View style={ {flex:1}}>
    <View style={ {flexDirection:'row',height:40}}>
        <View style={ {width:50,backgroundColor:"grey",justifyContent:"center",alignItems:"center"}}>
            <Text>返回</Text>
        </View>
        <View style={ {flex:1,backgroundColor:"white",justifyContent:"center",alignItems:"center"}}>
            <Text>嵌套網格</Text>
        </View>
        <View style={ {width:50,backgroundColor:"grey",justifyContent:"center",alignItems:"center"}}>
            <Text>確認</Text>
        </View>
    </View>
    <View style={ {flexDirection:'row',flex:1,backgroundColor:"#cccccc",justifyContent:"center",alignItems:"center"}}>
        <Text>do something...</Text>
    </View>
</View>

一個稍微復雜點的界面

下面通過實例運用前面介紹的幾個基本網格布局,以及上一篇文章介紹的布局基本知識實現一個稍微復雜點的例子,完成如下效果:

事先聲明,示例圖中上面的banner和底部的tab都是假的,只是用靜態圖片代替,因為本文的重點是介紹布局,當然后面會介紹banner以及tabnavigation的使用。

步驟
初看上去界面有點復雜,其實都是由一層層簡單的布局嵌套之后形成的,將它們一層層撥開,你心里可能會想也就那么回事。下面來帶領大家一步步構建出這樣的一個漫畫App首頁的布局效果。

整個頁面的框架

最外層是由一個上下的滑動視圖+固定高度的底部導航欄構成。因此該布局可以由主軸為豎直軸(flexDirection: "column"也可以不寫,因為默認主軸是豎直軸) 的父容器,子元素上部分是一個ScrollView,底部是一個高度固定的導航欄。

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

export default class ComicMainDemo extends Component {
    render() {
        return (
            <View style={styles.container}>
                <ScrollView>
                </ScrollView>
                <View style={styles.foot}>
                </View>
            </View>
        );
    }
}

const styles = {
    container: {
        flex: 1,
        backgroundColor: '#d0d0d0'
    },
    foot: {
        height: 50,
        backgroundColor: '#ffffff',
    },
};

效果圖如下:


底部導航欄

底部導航欄有四個圖片等分底部,其實這里我們有兩個思路:一個是用前面介紹的等分網格,為這四個子元素設置flex:1;另一種方法是給父元素設置justifyContent:'space-around'。這里采用第二種方法。
在前面的<View style={styles.foot}><View>中加入四張圖片:

<View style={styles.foot}>
    <Image style={styles.footimage} source={require('../images/home_boy.png')}/>
    <Image style={styles.footimage} source={require('../images/book_boy.png')}/>
    <Image style={styles.footimage} source={require('../images/ground_boy.png')}/>
    <Image style={styles.footimage} source={require('../images/mine_boy.png')}/>
</View>

然后為styles.foot加上 flexDirectionjustifyContent屬性,并添加styles.footimage的樣式:

foot: {
    height: 50,
    backgroundColor: '#ffffff',
    flexDirection: "row",
    justifyContent: "space-around"
},
footimage: {
    height: 50,
    width: 50
}

效果圖如下:

主體內容ScrollView

主體內容可以分為三個模塊,上:廣告欄區,中:分類按鈕區,下:推薦漫畫區。在<ScrollView></ScrollView>中加入這三個模塊

<ScrollView>
    <View style={styles.banner}>
    </View>
    <View style={styles.category}>
    </View>
    <View style={styles.recommend}>
    </View>
</ScrollView>

style樣式分別加上bannercategoryrecommend,其中bannercategory為固定高度,recommend填充剩余空間。

banner: {
    height: 200,
    backgroundColor: '#d0d0d0',
},
category: {
    height: 100,
    backgroundColor: '#ffffff',
    flexDirection: "row",
    justifyContent: "space-around",
    paddingTop: 10,

},
recommend: {
    flex: 1,
    backgroundColor: '#ffffff',
    marginTop: 8,
},

Reload JS,可以看到:

廣告欄

廣告欄區這里只用一個圖片表示,由于本文主要講解布局,所以就不用banner的組件(后面的文章會介紹用真正的banner做廣告欄的),只是用靜態圖片代替。
<View style={styles.banner}></View>中加入一張廣告圖片:

<View style={styles.banner}>
     <Image style={styles.bannerimage} source={{uri: 'https://manhua.qpic.cn/operation/0/15_00_20_3ffa389e446f5206e24c82ad5c24fd14_1500049213201.jpg/0'}}/>
</View>

style加上bannerimage的樣式:

bannerimage: {
    height: 200,
},

在看看效果:

已經有些雛形了有木有!

分類區

分類區也是五個子元素均分整個區域,類似底部的導航欄,前面介紹導航欄時我說有兩種思路,導航欄用了第二種,這里我就用第一種方法吧——均分網格。將每個子元素的flex都設為1。
<View style={styles.category}></View>中加入五個子元素

<View style={styles.category}>
    <View style={styles.categorybox}>
        <Image style={styles.categoryimage} source={require('../images/vip_account.png')}/>
        <Text style={styles.categorytext}>男生榜</Text>
    </View>
    <View style={styles.categorybox}>
        <Image style={styles.categoryimage} source={require('../images/user_crown_is_vip.png')}/>
        <Text style={styles.categorytext}>人氣榜</Text>
    </View>
    <View style={styles.categorybox}>
        <Image style={styles.categoryimage} source={require('../images/vip_finish.png')}/>
        <Text style={styles.categorytext}>熱賣榜</Text>
    </View>
    <View style={styles.categorybox}>
        <Image style={styles.categoryimage} source={require('../images/vip_cloud.png')}/>
        <Text style={styles.categorytext}>排行榜</Text>
    </View>
    <View style={styles.categorybox}>
        <Image style={styles.categoryimage} source={require('../images/color_for_danmu_select.png')}/>
        <Text style={styles.categorytext}>分類</Text>
    </View>
</View>

加上它們對應的style樣式:

categorybox: {
    flex: 1,
    backgroundColor: '#ffffff',
    alignItems: "center"
},
categoryimage: {
    height: 50,
    width: 50,
},
categorytext: {
    alignSelf: 'center',
    fontSize: 12,
    color: '#333333',
},

再次Reload看看效果:

已經越來越是那么回事了!

推薦區

推薦區這里又分為兩部分,上方的小標題欄,以及下方的圖片展示區。
小標題欄這里我們采用了前面介紹的兩邊固定中間填充的網格,
<View style={styles.recommend}></View>中插入推薦小標題欄:

<View style={styles.recommend}>
    <View style={styles.recommendtitlecontainer}>
        <View style={styles.recommendspaceview}></View>
        <View style={styles.recommendtitle}>
            <Text style={styles.recommendtitletext}>主編推薦</Text>
        </View>
        <View style={styles.recommendspaceview}>
            <Image style={styles.recommendmoreimage} source={require('../images/more_boy.png')}/>
        </View>
    </View>
</View>

相應的style樣式:

recommendtitlecontainer: {
    height: 50,
    justifyContent:"center",
    flexDirection:"row"
},
recommendspaceview: {
    justifyContent:"center",
    width:100,
},
recommendtitle: {
    flex:1,
    justifyContent:"center",
},
recommendtitletext: {
    alignSelf: 'center',
    fontSize: 14,
    color: '#333333',
},
recommendmoreimage: {
    height: 35,
    width:80,
    alignSelf: 'center',
},

接下來是最后的圖片展示區了,在<View style={styles.recommend}></View>中,在<View style={styles.recommend}></View>下面加入<View style={styles.recommendcomic}></View>

<View style={styles.recommendcomic}>
    <Image style={styles.recommendcomicimage}
           source={{uri: 'https://manhua.qpic.cn/vertical/0/21_14_21_96ed95f31667b3966cb0e0521ce13703_1498026084112.jpg/420'}}/>
    <Image style={styles.recommendcomicimage}
           source={{uri: 'https://manhua.qpic.cn/vertical/0/21_14_21_96ed95f31667b3966cb0e0521ce13703_1498026084112.jpg/420'}}/>
    <Image style={styles.recommendcomicimage}
           source={{uri: 'https://manhua.qpic.cn/vertical/0/21_14_21_96ed95f31667b3966cb0e0521ce13703_1498026084112.jpg/420'}}/>
    <Image style={styles.recommendcomicimage}
           source={{uri: 'https://manhua.qpic.cn/vertical/0/21_14_21_96ed95f31667b3966cb0e0521ce13703_1498026084112.jpg/420'}}/>
    <Image style={styles.recommendcomicimage}
           source={{uri: 'https://manhua.qpic.cn/vertical/0/21_14_21_96ed95f31667b3966cb0e0521ce13703_1498026084112.jpg/420'}}/>
    <Image style={styles.recommendcomicimage}
           source={{uri: 'https://manhua.qpic.cn/vertical/0/21_14_21_96ed95f31667b3966cb0e0521ce13703_1498026084112.jpg/420'}}/>
</View>

最后,我們再在styles對象里給圖片展示區添加相應的樣式。

recommendcomic: {
    flex: 1,
    backgroundColor: '#ffffff',
    flexDirection: "row",
    justifyContent: "space-around",
    marginTop: 5,
    flexWrap:"wrap",
    paddingBottom:10,
},
recommendcomicimage: {
    height: 160,
    width:140,
    marginTop:10,
},

再來看看最終的效果吧:

是不是有些小小的成就感,當然真正的應用布局遠比這個Demo復雜,本文只是提供一個運用前面基礎布局知識的思路, 算是拋磚引玉吧。本文中的頂部的廣告欄和底部導航欄都只是用了簡單的圖片替代,并沒實現真正的效果。后面我會專門介紹navigation及spanner來實現導航欄和廣告欄的。

最后附上本節的完整代碼
https://github.com/mronion0603/ReactNativeExercise/blob/master/src/02_flex/ComicMainDemo.js

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,412評論 6 532
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,514評論 3 416
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 176,373評論 0 374
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,975評論 1 312
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,743評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,199評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,262評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,414評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,951評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,780評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,983評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,527評論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,218評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,649評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,889評論 1 286
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,673評論 3 391
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,967評論 2 374

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,653評論 25 708
  • 前言 學習本系列內容需要具備一定 HTML 開發基礎,沒有基礎的朋友可以先轉至 HTML快速入門(一) 學習 本人...
    珍此良辰閱讀 7,301評論 33 15
  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標簽默認的外補...
    _Yfling閱讀 13,776評論 1 92
  • 大家下午好今天那就是課件的相對來說會長一點,那我就不需就不用文字了,我就用語音和大家說說吧。 希望大家就是一定要認...
    劉小清閱讀 629評論 0 0
  • 本文參考自Spring官方文檔 Spring EL。 在Java上有很多表達式語言,在很多領域有各種各樣的應用。我...
    樂百川閱讀 11,913評論 0 8