學習React Native日記四,樣式和高度與寬度

樣式

所有的核心組件都接受名為style的屬性
基本遵循Web的CSS命名,不過因為在JS中所以要求以駝峰法命名。
數組中靠后的樣式優先級更高
一般使用 StyleSheet.create 集中定義組件樣式

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

class SomeStyles extends Component{
  render(){
    return (
    <View>
    <Text style={styles.red}> red</Text>
    <Text style={[styles.bigblue, styles.red]}>bigblue, then red</Text>
    </View>
  );}
}

const styles = StyleSheet.create({
  bigblue : { 
    color: 'blue', 
    fontWeight: 'bold', 
    fontSize: 30, 
    }, 
    red: { 
    color: 'red', 
    },
});

AppRegistry.registerComponent('SomeStyles ', () => SomeStyles );

高度與寬度

組件的高寬決定屏幕顯示尺寸。

1.指定寬高
2.彈性寬高

指定寬高

即width和height。

尺寸是無單位的,指代像素邏輯點。

class FixedWidthAndHeight extends Component{
  render(){
    return (
      <View> 
        <View style={{width: 50, height: 50, backgroundColor: 'bigblue '}} />
        <View style={{width: 100, height: 100, backgroundColor: 'red'}} />
      </View>
    );
  }
}//一般用于不同尺寸的屏幕上都顯示成一樣的大小。

彈性寬高

即通過設置比例動態擴張或收縮。

沒有固定的width和height,也沒有設定flex。即尺寸為0。
父容器的尺寸不可為0,否則無法顯示。

class FlexWidthAndHeight extends Component{
  render(){
    return (
      <View style={{flex: 1}}>
        <View style={{flex: 1, backgroundColor: 'bigblue'}} /> 
        <View style={{flex: 3, backgroundColor: 'red'}} /> 
        <View style={{flex: 1,  backgroundColor: 'bigblue'}} /> 
      </View>
    );
  }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標簽默認的外補...
    _Yfling閱讀 13,805評論 1 92
  • <一>編寫Hello World React Native看起來很像React,只不過其基礎組件是原生組件而非we...
    AFinalStone閱讀 1,040評論 0 2
  • 選擇qi:是表達式 標簽選擇器 類選擇器 屬性選擇器 繼承屬性: color,font,text-align,li...
    wzhiq896閱讀 1,805評論 0 2
  • 選擇qi:是表達式 標簽選擇器 類選擇器 屬性選擇器 繼承屬性: color,font,text-align,li...
    love2013閱讀 2,337評論 0 11
  • 小時候電視看膩了,大了連開都不開一下![捂臉] 物質匱乏的年代搞出來的東西多少走心!物質文明精神文明沒有一道同步,...
    縱情嬉戲天地間閱讀 250評論 0 0