React-native之Flexbox

本文總結:

我們學到了 React Native 的 Flexbox 布局,它讓寫樣式變得更方便啦!😊 Flexbox 就像一個有彈性的盒子,有主軸和交叉軸(行或列)。

在 RN 里寫樣式要用 StyleSheet.create 對象,屬性名是駝峰命名

文章還介紹了如何用 Platform.select 給不同平臺寫樣式,以及使用 styled-components 這個庫的簡單例子(雖然我們主要還是用 StyleSheet)。

文章還通過三欄布局橫向布局的例子,展示了 flexDirection, alignItems, justifyContent alignSelf 這些屬性的用法。

特別喜歡 alignSelf: 'stretch' 能讓元素填充空間的技巧!👍

最后,通過抽象出 RowCol 組件,我看到了如何用 Flexbox 實現更復雜的嵌套布局。感覺 Flexbox 真的讓布局變得靈活多了!🚀

1、關于Flexbox

在將flexbox引入css前,構建布局的各種css屬性都比較粗糙,而且很容易出錯。而flexbox通過抽象了很多屬性來解決問題。字如其名flexbox的意思就是一個具有彈性的盒子模型。我們畫個圖:假設你有一個容器和它的子元素,它看起來可以是這樣的。

Flexbox容器有二個軸,即(向上/向下)或(左/右)。

2、實操

我們直接上代碼吧,這是一個js模塊,而不是css模塊。如果我們要聲明rn的樣式,我們需要去定義一個對象然后調用StyleSheet.create()最后拋出它在你的模塊中。但是我們需要注意的是rn只支持駝峰命名

import { Platform, StyleSheet, StatusBar } from 'react-native'
export default StyleSheet.create({container: {flex: 1,justifyContent: 'center',alignItems: 'center',backgroundColor: 'ghostwhite',...Platform.select({ios: { paddingTop: 20 },android: { paddingTop: StatusBar.currentHeight },}),},box: {width: 100,height: 100,justifyContent: 'center',alignItems: 'center',backgroundColor: 'lightgray',},boxText: {color: 'darkslategray',fontWeight: 'bold',},
})

然后我們看到剛才我寫了這樣一段代碼,這里其實就是根據你的移動端去選擇樣式。

...Platform.select({ios: { paddingTop: 20 },android: { paddingTop: StatusBar.currentHeight },
}),

ok,我們看一下在rn組件中如何使用

import { Text, View } from 'react-native'
import styles from './styles'
export default function App() {return (<View style={styles.container}><View style={styles.box}><Text style={styles.boxText}>I'm in a box</Text></View></View>)
}

這些樣式將通過樣式屬性分配給每個組件。我們來看看它的表現。

3、Styled-components樣式組件庫使用

Styled-components是一個css-in-js的庫為我們的組件去提供樣式,如我們去直接看看怎么使用吧。當然只是介紹一下,我們還是使用styleSheet

首先下載依賴


yarn add styled-components

然后我們寫點代碼

import styled from "styled-components/native";
const Box = styled.View'width: 100px;height: 100px;justify-content: center;align-items: center;background-color: lightgray;
';
const BoxText = styled.Text'color: darkslategray;font-weight: bold;
';

使用

const App = () => {
return (<Box><BoxText>I'm in a box</BoxText></Box>
);};

4、基礎Flexbox

接下來主要講一下rn中的幾種常見布局和flexbox的實戰

4.1、三欄布局

普通的從上到下的三欄布局。

你可以把view理解成div,text理解成p

import { Text, View } from 'react-native'
import styles from './styles'
export default function App() {return (<View style={styles.container}><View style={styles.box}><Text style={styles.boxText}>#1</Text></View><View style={styles.box}><Text style={styles.boxText}>#2</Text></View><View style={styles.box}><Text style={styles.boxText}>#3</Text></View></View>)
}

flexDirection屬性是決定了主軸的方向,上到下或者左到右,而alignItemjustifyContent屬性決定了元素的排列和間隔。


import { Platform, StyleSheet, StatusBar } from 'react-native'
export default StyleSheet.create({container: {flex: 1,flexDirection: 'column',alignItems: 'center',justifyContent: 'space-around',backgroundColor: 'ghostwhite',...Platform.select({ios: { paddingTop: 20 },android: { paddingTop: StatusBar.currentHeight },}),},box: {width: 300,height: 100,justifyContent: 'center',alignItems: 'center',backgroundColor: 'lightgray',borderWidth: 1,borderStyle: 'dashed',borderColor: 'darkslategray',},boxText: {color: 'darkslategray',fontWeight: 'bold',},
})

而如果我們想讓它左右兩邊填滿那?就像這樣

我們可以加入alignSelf這個屬性,這個屬性的意思是根據主軸flexDirection的方向,改變寬度或者高度(column改變的就是寬度,row改變的就是高度)去填充空白,動態計算高度或寬度。像這樣就是會填滿你屏幕的寬度。

  box: {height: 100,justifyContent: 'center',// alignSelf: 'stretch',alignItems: 'center',backgroundColor: 'lightgray',borderWidth: 1,borderStyle: 'dashed',borderColor: 'darkslategray',},

當我們把手機橫過去

我們稍微優化一下,正好也寫一下橫向的布局,上面的樣式太`抽象`了寫得。

 

import { Text, View, StatusBar } from 'react-native'
import styles from './styles'
import Box from './Box'
export default function App() {return (<View style={styles.container}><Box>#1</Box><Box>#2</Box></View>)
}

import { PropTypes } from 'prop-types'
import { View, Text } from 'react-native'
import styles from './styles'
export default function Box({ children }) {return (<View style={styles.box}><Text style={styles.boxText}>{children}</Text></View>)
}
Box.propTypes = {children: PropTypes.node.isRequired,
}

這個就是會拉伸至整個屏幕高度的橫向布局

 

import { Platform, StyleSheet, StatusBar } from 'react-native'
export default StyleSheet.create({container: {flex: 1,flexDirection: 'row',backgroundColor: 'ghostwhite',alignItems: 'center',justifyContent: 'space-around',...Platform.select({ios: { paddingTop: 20 },android: { paddingTop: StatusBar.currentHeight },}),},box: {width: 100,justifyContent: 'center',alignSelf: 'stretch',alignItems: 'center',backgroundColor: 'lightgray',borderWidth: 1,borderStyle: 'dashed',borderColor: 'darkslategray',},boxText: {color: 'darkslategray',fontWeight: 'bold',},
})

我們來看看它的表現

當我們把手機橫過去

5、稍微復雜一點的flexBox

假設我們要實現這樣一個效果,我們該如何實現?

在我們的意識中,整個布局有。那我們同樣也可以抽象出colrow組件分別代表行列。我們直接上代碼吧。

可以看到我們分別確定了colrow的方向和排列。

import { Platform, StyleSheet, StatusBar } from 'react-native'export default StyleSheet.create({container: {flex: 1,flexDirection: 'column',backgroundColor: 'ghostwhite',alignItems: 'center',justifyContent: 'space-around',...Platform.select({ios: { paddingTop: 40 },android: { paddingTop: StatusBar.currentHeight },}),},box: {height: 100,width: 100,justifyContent: 'center',alignItems: 'center',borderWidth: 1,borderStyle: 'dashed',borderColor: 'darkslategray',backgroundColor: 'lightgray',},boxText: {color: 'darkslategray',fontWeight: 'bold',},row: {flex: 1,flexDirection: 'row',justifyContent: 'space-around',alignSelf: 'stretch',},column: {flex: 1,flexDirection: 'column',alignItems: 'center',justifyContent: 'space-around',alignSelf: 'stretch',},
})

組件部分

app

import { View, StatusBar } from 'react-native'
import styles from './styles'
import Row from './Row'
import Col from './Col'
import Box from './Box'
export default function App() {return (<View style={styles.container}><StatusBar hidden={false} /><Row><Col><Box>#1</Box><Box>#2</Box></Col><Col><Box>#3</Box><Box>#4</Box></Col></Row><Row><Col><Box>#5</Box><Box>#6</Box></Col><Col><Box>#7</Box><Box>#8</Box></Col></Row><Row><Col><Box>#9</Box><Box>#10</Box></Col><Col><Box>#11</Box><Box>#12</Box></Col></Row></View>)
}

col


import PropTypes from 'prop-types'
import { View } from 'react-native'
import styles from './styles'
export default function Column({ children }) {return <View style={styles.column}>{children}</View>
}Column.propTypes = {children: PropTypes.node.isRequired,
}

row


import PropTypes from 'prop-types'
import { View } from 'react-native'
import styles from './styles'
export default function Row({ children }) {return <View style={styles.row}>{children}</View>
}Row.propTypes = {children: PropTypes.node.isRequired,
}

import React from 'react'
import PropTypes from 'prop-types'
import { View, Text } from 'react-native'
import styles from './styles'
export default function Box({ children }) {return (<View style={styles.box}><Text style={styles.boxText}>{children}</Text></View>)
}Box.propTypes = {children: PropTypes.node.isRequired,
}

6、總結

歡迎加入群聊,我們一起討論一些更有趣的技術、商業、閑聊。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/pingmian/83438.shtml
繁體地址,請注明出處:http://hk.pswp.cn/pingmian/83438.shtml
英文地址,請注明出處:http://en.pswp.cn/pingmian/83438.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Leetcode 1336. 每次訪問的交易次數

1.題目基本信息 1.1.題目描述 表: Visits ---------------------- | Column Name | Type | ---------------------- | user_id | int | | visit_date | date | ---------------------- (user_id, visit_date) 是該表的主鍵(具有唯一值的列的組合) 該表的每行表示 use…

騰訊云國際版和國內版賬戶通用嗎?一樣嗎?為什么?

在當今全球化的數字化時代&#xff0c;云計算服務成為眾多企業和個人拓展業務、存儲數據的重要選擇。騰訊云作為國內領先的云服務提供商&#xff0c;其國際版和國內版備受關注。那么&#xff0c;騰訊云國際版和國內版賬戶是否通用&#xff1f;它們究竟一樣嗎&#xff1f;背后又…

解鎖Java多級緩存:性能飛升的秘密武器

一、引言 文末有彩蛋 在當今高并發、低延遲的應用場景中&#xff0c;傳統的單級緩存策略往往難以滿足性能需求。隨著系統規模擴大&#xff0c;數據訪問的瓶頸逐漸顯現&#xff0c;如何高效管理緩存成為開發者面臨的重大挑戰。多級緩存架構應運而生&#xff0c;通過分層緩存設…

Android Kotlin 算法詳解:鏈表相關

前言 &#x1f60a; 在 Android 開發中&#xff0c;算法與數據結構是基本功之一&#xff0c;而鏈表&#xff08;Linked List&#xff09;作為常見的數據結構&#xff0c;經常出現在各類面試題與實際業務場景中。本文將以 Android Kotlin 為語言&#xff0c;結合 LeetCode 上的…

Blinko智能筆記系統實現跨平臺同步與隱私保護的完整技術方案解析

文章目錄 前言1. Docker Compose一鍵安裝2. 簡單使用演示3. 安裝cpolar內網穿透4. 配置公網地址5. 配置固定公網地址 推薦 ? 前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。 點擊跳轉到網站 前言 是否…

【小紅書】API接口,獲取筆記列表

小紅書筆記列表API接口詳解 - 深圳小于科技助力高效數據對接 深圳小于科技&#xff08;官網&#xff1a;https://www.szlessthan.com&#xff09;提供的小紅書筆記列表API接口&#xff0c;幫助開發者快速獲取小紅書平臺筆記數據&#xff0c;實現高效內容管理與分析。 接口概述…

Qwen2.5-VL 損失函數

Qwen2.5-VL 損失函數 flyfish 文章名稱鏈接深入理解交叉熵損失 CrossEntropyLoss - 概率基礎鏈接深入理解交叉熵損失 CrossEntropyLoss - 對數鏈接深入理解交叉熵損失 CrossEntropyLoss - 概率分布鏈接深入理解交叉熵損失 CrossEntropyLoss - 信息論&#xff08;交叉熵&#…

LeetCode 1524. 和為奇數的子數組數目

好的&#xff01;讓我們詳細解釋 LeetCode 1524. 和為奇數的子數組數目 這道題的思路和解法。 題目&#xff1a; https://leetcode.cn/problems/number-of-sub-arrays-with-odd-sum/description/ 題目分析 問題描述&#xff1a; 給定一個整數數組 arr&#xff0c;返回其中和…

kafka學習筆記(三、消費者Consumer使用教程——消費性能多線程提升思考)

1.簡介 KafkaConsumer是非線程安全的&#xff0c;它定義了一個acquire()方法來檢測當前是否只有一個線程在操作&#xff0c;如不是則會拋出ConcurrentModifcationException異常。 acquire()可以看做是一個輕量級鎖&#xff0c;它僅通過線程操作計數標記的方式來檢測線程是否發…

百度蜘蛛池的作用是什么?技術@baidutopseo

百度蜘蛛池的作用 百度蜘蛛池是一種用于集中管理和調度百度搜索引擎蜘蛛&#xff08;爬蟲&#xff09;的技術工具。其主要目的是優化網站內容的收錄效率&#xff0c;幫助搜索引擎更高效地發現和抓取網頁內容。技術baidutopseo 提升收錄效率 通過集中大量百度蜘蛛的訪問請求&…

黑馬Java面試筆記之 消息中間件篇(Kafka)

一. Kafka保證消息不丟失 Kafka如何保證消息不丟失 使用Kafka在消息的收發過程中都會出現消息丟失&#xff0c;Kafka分別給出了解決方案 生產者發送消息到Brocker丟失消息在Brocker中存儲丟失消費者從Brocker接收消息丟失 1.1 生產者發送消息到Brocker丟失 設置異步發送 消息…

dis css port brief 命令詳細解釋

華為交換機命令 display css port brief 詳細解釋 display css port brief 是華為交換機中用于 快速查看堆疊&#xff08;CSS&#xff0c;Cluster Switch System&#xff09;端口狀態及關鍵參數 的命令&#xff0c;適用于日常運維、堆疊鏈路健康檢查及故障定位。以下是該命令的…

Redis 緩存問題及其解決方案

1. 緩存雪崩 概念&#xff1a;緩存雪崩是指在緩存層出現大范圍緩存失效或緩存服務器宕機的情況下&#xff0c;大量請求直接打到數據庫&#xff0c;導致數據庫壓力驟增&#xff0c;甚至可能引發數據庫宕機。 影響&#xff1a;緩存雪崩會導致系統性能急劇下降&#xff0c;甚至導…

使用Python進行函數作畫

前言 因為之前通過deepseek繪制一下卡通的人物根本就不像&#xff0c;又想起來之前又大佬通過函數繪制了一些圖像&#xff0c;想著能不能用Python來實現&#xff0c;結果發現可以&#xff0c;不過一些細節還是需要自己調整&#xff0c;deepseek整體的框架是沒有問題&#xff0…

關于list集合排序的常見方法

目錄 1、list.sort() 2、Collections.sort() 3、Stream.sorted() 4、進階排序技巧 4.1 空值安全處理 4.2 多字段組合排序 4.3. 逆序 5、性能優化建議 5.1 并行流加速 5.2 原地排序 6、最佳實踐 7、注意事項 前言 Java中對于集合的排序操作&#xff0c;分別為list.s…

Java高級 | (二十二)Java常用類庫

參考&#xff1a;Java 常用類庫 | 菜鳥教程 一、核心Java類庫 二、常用第三方庫 以下是 Java 生態系統中廣泛使用的第三方庫&#xff1a; 類別庫名稱主要功能官方網站JSON 處理JacksonJSON 序列化/反序列化https://github.com/FasterXML/jacksonGsonGoogle 的 JSON 庫https:…

幾種常用的Agent的Prompt格式

一、基礎框架范式&#xff08;Google推薦標準&#xff09; 1. 角色與職能定義 <Role_Definition> 你是“項目專家”&#xff08;Project Pro&#xff09;&#xff0c;作為家居園藝零售商的首席AI助手&#xff0c;專注于家裝改造領域。你的核心使命&#xff1a; 1. 協助…

蛋白質結構預測軟件openfold介紹

openfold 是一個用 Python 和 PyTorch 實現的 AlphaFold2 的開源復現版&#xff0c;旨在提升蛋白質結構預測的可復現性、可擴展性以及研究友好性。它允許研究者在不開源 DeepMind 原始代碼的情況下&#xff0c;自由地進行蛋白結構預測的訓練和推理&#xff0c;并支持自定義模型…

AD轉嘉立創EDA

可以通過嘉立創文件遷移助手進行格式的轉換 按照它的提示我們整理好文件 導出后是這樣的&#xff0c;第一個文件夾中有原理圖和PCB&#xff0c;可以把它們壓縮成一個壓縮包 這個時候我們打開立創EDA&#xff0c;選擇導入AD 這樣就完成了

MySQL(50)如何使用UNSIGNED屬性?

在 MySQL 中&#xff0c;UNSIGNED 屬性用于數值數據類型&#xff08;如 TINYINT、SMALLINT、MEDIUMINT、INT 和 BIGINT&#xff09;&#xff0c;表示該列只能存儲非負整數。使用 UNSIGNED 屬性可以有效地擴展列的正整數范圍&#xff0c;因為它不需要為負數保留空間。 1. 定義與…