如何在react-native上使用不同的android和ios样式?

lym*_*ght 6 javascript android ios reactjs react-native

我想为ios和android使用不同的样式,我该怎么办呢?也许有人知道如何设置TextInput的样式,我只需要底部边框,但borderBottomWidth不起作用.

Emi*_*uez 10

有很多方法可以实现这一目标.在您的情况下最简单的是使用Platform.OS:

var {Platform} = React;

var styles = StyleSheet.create({
    height: (Platform.OS === 'ios') ? 200 : 100
});
Run Code Online (Sandbox Code Playgroud)


onm*_*133 7

https://facebook.github.io/react-native/docs/platform-specific-code,您可以使用Platform.select

还有一个 Platform.select 方法可用,给定一个包含 Platform.OS 作为键的对象,返回您当前运行的平台的值。

import { Platform, StyleSheet } from 'react-native'

const styles = StyleSheet.create({
  container: {
    flex: 1,
    ...Platform.select({
      ios: {
        backgroundColor: 'red',
      },
      android: {
        backgroundColor: 'blue',
      },
    }),
  }
})
Run Code Online (Sandbox Code Playgroud)