React Native中的CSS的三种使用方式

一、Web端CSS的使用

CSS是Cascading Style Sheet(层叠样式表)的缩写。是用于(增强)控制网页样式并允许将样式信息与网页内容分离的一种标记性语言。

如何将样式表加入您的网页:

你可以用以下三种方式将样式表加入您的网页。而最接近目标的样式定义优先权越高。高优先权样式将继承低优先权样式的未重叠定义但覆盖重叠的定义。

内联方式 Inline Styles 内联定义即是在对象的标记内使用对象的style属性定义适用其的样式表属性。 示例代码:Snip20160902_1

内部样式块对象 Embedding a Style Block 你可以在你的HTML文档的<head>标记里插入一个<style>块对象。 示例代码:

Snip20160902_4

外部样式表 Linking to a Style Sheet 你可以先建立外部样式表文件*.css,然后使用HTML的link对象。 示例代码:

Snip20160902_6

二、React Native中CSS

内联样式

Snip20160902_9

对象样式

Snip20160902_10 Snip20160902_11

使用Stylesheet.Create

Snip20160902_12 Snip20160902_13

样式拼接

Snip20160902_14

导出样式对象

下面的代码是index.ios.js中的代码:

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

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

import liyuechun from './styles'

var textStyle = {
  fontSize:15,
  backgroundColor:'#DAC',
  textAlign:'center'
};

class YZDemo extends Component {
  render() {
    return (
      <View style={
        {
          width: 300,
          height: 600,
          backgroundColor: 'red',
          padding: 50,
          margin: 10
        }
      }>

        <Text style={textStyle}>
          www.52learn.wang
        </Text>  

        <Text style={liyuechun.yuzhi}>
          匠心品质
        </Text>

        <Text style={liyuechun.liangxin}>
          良心育人
        </Text>

        <Text style={[liyuechun.textFontSize, liyuechun.textBGColor]}>
          黎跃春
        </Text>

        <Text style={[liyuechun.textFontSize, {backgroundColor: 'yellow'}]}>
          欢迎大家来到育知同创
        </Text>
      </View>
    );
  }
}

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

下面的代码是styles.js的代码:

import React from 'react';
import {
  StyleSheet
} from 'react-native';

var liyuechun = StyleSheet.create(
{
  yuzhi: {
    fontSize: 40,
    color: 'green',
    marginTop: 40,
    backgroundColor: 'blue'
  },

  liangxin: {
    fontSize: 80,
    backgroundColor: '#FFF',
    marginTop: 5
  },

  textFontSize: {
    fontSize: 20
  },
  textBGColor: {
    backgroundColor: '#F88'
  }
});

module.exports = liyuechun;

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
React Native 使用类似于 CSS 的样式语法来定义组件的样式。你可以使用内联样式对象,也可以使用外部样式表文件来管理样式。 1. 内联样式:在组件的 `style` 属性直接定义样式对象,例如: ```jsx import React from 'react'; import { View, Text, StyleSheet } from 'react-native'; const MyComponent = () => { return ( <View style={styles.container}> <Text style={styles.text}>Hello, React Native!</Text> </View> ); }; const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', }, text: { fontSize: 20, fontWeight: 'bold', color: 'blue', }, }); export default MyComponent; ``` 2. 外部样式表:将样式定义在外部的样式表文件,然后在组件引入使用,例如: ```jsx // styles.js import { StyleSheet } from 'react-native'; const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', }, text: { fontSize: 20, fontWeight: 'bold', color: 'blue', }, }); export default styles; // MyComponent.js import React from 'react'; import { View, Text } from 'react-native'; import styles from './styles'; const MyComponent = () => { return ( <View style={styles.container}> <Text style={styles.text}>Hello, React Native!</Text> </View> ); }; export default MyComponent; ``` 以上是一些基本的示例,你可以在样式对象设置各种属性,例如字体大小、颜色、布局等。还可以使用 `StyleSheet` 提供的一些辅助函数,比如 `create` 来创建样式表、`flatten` 来合并多个样式对象等。具体的样式属性和特性可以参考 React Native 的官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值