react-native 之style

react-native 不是通过css来实现app的styles,而是依赖于JavaScript。

一、声明一个styles:

var styles = StyleSheet.create({

base: {

width: 38,

height: 38,

},

background: {

backgroundColor: '#222222',

},

active:

{

borderWidth: 2, borderColor: '#00ff00',

},

});

查看其他属性的名称,可以参考:http://facebook.github.io/react-native/docs/flexbox.html

二、使用styles

1、所有的核心组件都能应用styles

<Text style={styles.base} />

<View style={styles.background} />

2、也可以使用数组来限制一个组件

<View style={[styles.base, styles.background]} />

3、通常情况下,会在某个特定情况下加上styles

<View style={[styles.base, this.state.active && styles.active]} />

不鼓励在render中加入styles。

三、样式传递

为了让一个 call site 定制你的子组件的样式,你可以通过样式传递。使用 View.propTypes.style 和 Text.propTypes.style,以确保只有样式被传递了。

var List = React.createClass({
  propTypes: {
    style: View.propTypes.style,
    elementStyle: View.propTypes.style,
  },
  render: function() {
    return (
      <View style={this.props.style}>
        {elements.map((element) =>
          <View style={[styles.element, this.props.elementStyle]} />
        )}
      </View>
    );
  }
});
// ... in another file ...
<List style={styles.list} elementStyle={styles.listElement} />
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值