简体   繁体   English

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

[英]How to use different styles for android and ios on react-native?

I want to use different styles for ios and android, how can I do it?我想为 ios 和 android 使用不同的样式,我该怎么做? And maybe somebody know how to stylizing TextInput, I need only bottom border, but borderBottomWidth doesnt work.也许有人知道如何风格化 TextInput,我只需要底部边框,但 borderBottomWidth 不起作用。

There are many ways to achieve this.有很多方法可以实现这一目标。 The simplest one in your case would be to use Platform.OS:在您的情况下,最简单的方法是使用 Platform.OS:

 var {Platform} = React; var styles = StyleSheet.create({ height: (Platform.OS === 'ios') ? 200 : 100 });

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

There is also a Platform.select method available, that given an object containing Platform.OS as keys, returns the value for the platform you are currently running on.还有一个 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',
      },
    }),
  }
})
import { Platform, StyleSheet } from 'react-native'

const styles = StyleSheet.create({
    logo: {
        width: (Platform.OS === 'ios') ? 80 : 100,
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM