ReactNative學(xué)習(xí)筆記4

樣式

原文地址,
原文地址2,本文為原文不完全翻譯
在ReactNative,是通過使用JavaScript來設(shè)置樣式的,全部的核心組件都擁有style這個屬性,這些樣式的名字和值都和CSS一一對應(yīng),唯一的不同就是ReactNative使用的是駝峰命名,如backgroundColor而不是background_color.
style屬性和JavaScript是一樣的,下面的例子就是style的簡單使用,可以見到style是可以接收一個數(shù)組的,不過這時候數(shù)組的最后一個成員擁有最高優(yōu)先權(quán),所以你可以利用這個特性來集成與改寫style
為了代碼的整潔性,一般都是使用StyleSheet.create方法在一個地方定義好幾個style

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

class alotStyles extends Component {
  render(){
    return (
      <View>
        <Text style={styles.red}>justred</Text>
        <Text style={styles.bigred}>bigred</Text>
        <Text style={styles.bigblue}>bigblue</Text>
        <Text style={styles.red,styles.bigblue,styles.blue}>mixture</Text>
      </View>
    )
  }
}

const styles = StyleSheet.create({
  red:{
    color:'#FF0000'
  },
  blue:{
    color:'#0000FF'
  },
  bigred:{
    color:'#FF0000',
    fontSize:30,
    fontWeight:'bold'
  },
  bigblue:{
    color:'#0000FF',
    fontSize:30,
    fontWeight:'bold'
  },
})

AppRegistry.registerComponent('alotStyles',()=>alotStyles);
Paste_Image.png

寬與高

一個組件的寬與高決定了組件在屏幕上面的尺寸

固定尺寸

控制組件位置最簡單的方式就是給它一個固定的寬高,ReactNative的組件尺寸單位是dip(設(shè)備無關(guān)像素)

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>
    );
  }
}

AppRegistry.registerComponent('AwesomeProject', () => FixedDimensionsBasics);
Paste_Image.png

這種寫法只適用于尺寸在各個大小不同的手機(jī)屏幕上都一樣的情況,所以用得不多.

可變尺寸

適用屬性flex可以讓組件在一個有效的空間內(nèi)按比例縮放,一般情況下我們賦值flex=1,這意味著讓組件盡可能地填充有效空間,并且根據(jù)在同一個父控件中的兄弟控件的flex的值等比例分配空間

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

class FlexDimensionsBasics extends Component{
  render(){
     return(<View style={{height:100, width:100}}>
              <View style={{flex:1, backgroundColor: 'powderblue'}} />
              <View style={{flex:1, backgroundColor: 'skyblue'}} />
              <View style={{flex:1, backgroundColor: 'steelblue'}} />
           </View>)
  }
}
AppRegistry.registerComponent('FlexDimensionsBasics', () => FlexDimensionsBasics);
Paste_Image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,996評論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,219評論 4 61
  • 霓虹繁華的夜, 和你, 潛入暖意繞身的Bubble bar 看你, 纖指吞云吐霧, 唇畔踏海揚(yáng)杯。 想你, 此番模...
    洛梵華閱讀 639評論 0 0
  • 起程回京,每次回京的路,都是自己最不愿意獨(dú)自走的路!和親人的離別,是自己最不能承受的痛!再加上和皇上的關(guān)系,總讓自...
    二的平方閱讀 507評論 0 51
  • 當(dāng)我平靜的寫下這些文字時,窗外日光傾斜,回憶開始變得綿長,往事緩緩開落。 時間回到2014年夏天,就是現(xiàn)在的這個時...
    追天青年閱讀 635評論 0 1

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