首页 > 其他 > 详细

react-native 之style

时间:2016-02-22 15:57:44      阅读:316      评论:0      收藏:0      [点我收藏+]

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} />

react-native 之style

原文:http://www.cnblogs.com/wangbaixue/p/5207162.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!