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

转载于:https://www.cnblogs.com/wangbaixue/p/5207162.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React Native 是一种基于 React 构建的开源框架,用于构建跨平台移动应用(支持 iOS 和 Android)。它允许开发者使用 JavaScript 和 React 来编写原生应用界面组件,这使得开发过程更加高效并能共享代码库。 ### 基本概念 #### 组件 React Native 应用由一系列可重用的组件组成,每个组件代表 UI 中的一个部分。组件通过属性(props)传递参数给子组件,而状态(state)则用来存储组件内部的数据,可以根据状态变化来更新UI。 #### 生命周期 React Native 组件拥有生命周期方法,类似于 React.js,在创建、渲染、更新、卸载时触发相应的回调函数,如 `componentDidMount`、`componentWillReceiveProps`、`shouldComponentUpdate`、`componentDidUpdate` 和 `componentWillUnmount`。 #### 样式 样式可以通过自定义 StyleSheet 对象实现,也可以使用 CSS-in-JS 方法,如 styled-components 或者 react-native-styled-components。 ### 示例 - 创建简单的按钮 下面是一个简单的 React Native 示例,展示了如何创建一个按钮组件: ```javascript import React from 'react'; import { Button } from 'react-native'; const App = () => { return ( <Button title="点击我" onPress={() => console.log('Button clicked!')} // 可以在这里添加事件监听逻辑 /> ); }; export default App; ``` 在这个例子中,我们导入了 `Button` 组件来自 `react-native` 模块,并将其放置在一个根元素内。当用户点击按钮时,控制台会打印 "Button clicked!"。 ### 如何开始使用 React Native 1. **安装环境**:首先,确保已安装 Node.js 和 npm。然后,使用 `create-react-native-app` 或 `expo init` 命令来创建一个新的 React Native 项目。 ```bash npx create-react-native-app your-project-name ``` 2. **运行应用程序**:在项目目录中启动开发服务器并查看应用: ```bash cd your-project-name npm start ``` 3. **打开模拟器或连接设备**:通过 `adb`(Android)或 `ios` 命令来启动默认的 Android/iOS 模拟器或连接物理设备。 4. **开发和调试**:编辑 `.js` 文件并在设备上实时预览更改。 ### 相关问题: 1. 如何在 React Native 中处理触摸事件? 2. React Native 怎样集成第三方库? 3. 如何利用 React Native 开发跨平台桌面应用?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值