React Native 的高度與寬度設置


React Native中的尺寸都是無單位的,表示的是與設備像素密度無關的邏輯像素點。

import React, { Component } from 'react'; import { AppRegistry, View } from 'react-native'; class FixedDimensionsBasics extends Component { render() { return ( <View> <View style={{width: 50, height: 50, backgroundColor: 'powderblue'}} /> <View style={{width: 100, height: 100, backgroundColor: 'skyblue'}} /> <View style={{width: 150, height: 150, backgroundColor: 'steelblue'}} /> </View> ); } }; // 注冊應用(registerComponent)后才能正確渲染 // 注意:只把應用作為一個整體注冊一次,而不是每個組件/模塊都注冊 AppRegistry.registerComponent('AwesomeProject', () => FixedDimensionsBasics);

2、彈性(Flex)寬高
import React, { Component } from 'react'; import { AppRegistry, View } from 'react-native'; class FlexDimensionsBasics extends Component { render() { return ( // 試試去掉父View中的`flex: 1`。 // 則父View不再具有尺寸,因此子組件也無法再撐開。 // 然后再用`height: 300`來代替父View的`flex: 1`試試看? <View style={{flex: 1}}> <View style={{flex: 1, backgroundColor: 'powderblue'}} /> <View style={{flex: 2, backgroundColor: 'skyblue'}} /> <View style={{flex: 3, backgroundColor: 'steelblue'}} /> </View> ); } }; AppRegistry.registerComponent('AwesomeProject', () => FlexDimensionsBasics);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM