ios - 样式 react native 组件 - 当以像素定义时,一切都太大了

标签 ios css react-native

我正在构建一个 React native 应用程序(直到现在没有 iOS 开发经验)并且我正在努力正确布局。屏幕应如下所示:

enter image description here

但它看起来像这样: enter image description here

我已将边距设置为我想要的像素大小,但它们似乎太大了。我是否误解了 React Native 布局的方式?

该组件的代码:

import React, { Component, PropTypes } from 'react';
import {
  Text,
  View,
  Button,
  Image,
  TouchableHighlight,
  StyleSheet
} from 'react-native'

import Background from '../assets/background.png'
import SoundOn from '../assets/main_icon_sound.png'
import SoundOff from '../assets/main_icon_soundoff.png'
import imprintButton from '../assets/main_icon_imprint.png'
import storyButton from '../assets/main_icon_story.png'
import gameButton from '../assets/main_icon_game.png'

import Game from './Game'
import Imprint from './Imprint'
import Story from './Story'
import Sound from './Sound'

const styles = StyleSheet.create({
  background: {
    height: 700, width: 700
  },
  soundButton: {
    marginLeft: 60, marginTop: 40
  },
  imprintButton: {
    marginLeft: 60, marginTop: 40
  },
  storyButton: {
    marginLeft: 382, marginBottom: 89
  },
  gameButton: {
    marginRight: 382, marginBottom: 89
  }
})

class MainMenu extends Component {
  static propTypes = {
    navigator: PropTypes.object.isRequired,
  }

  render() {
    return (
      <View>
        <Image source={Background} resizeMode={Image.resizeMode.contain} style={styles.background}>
          <View style={{backgroundColor: 'rgba(0,0,0,0)'}}>
            <TouchableHighlight style={styles.soundButton}>
              <Image source={SoundOn} resizeMode={Image.resizeMode.contain}></Image>
            </TouchableHighlight>

            <TouchableHighlight style={styles.imprintButton}>
              <Image source={imprintButton}></Image>
            </TouchableHighlight>

            <TouchableHighlight style={styles.storyButton}>
              <Image source={storyButton}></Image>
            </TouchableHighlight>

            <TouchableHighlight style={styles.gameButton}>
              <Image source={gameButton}></Image>
            </TouchableHighlight>
          </View>
        </Image>
      </View>
    )
  }
}

export default MainMenu

最佳答案

对于背景图片,我会使用 flexbox 和样式,

const styles = StyleSheet.create({
    bgImageWrapper: {
        position: 'absolute',
        top: 0, bottom: 0, left: 0, right: 0
    },
    bgImage: {
        flex: 1,
        resizeMode: "stretch"
    },
});

<View style={styles.bgImageWrapper}>
    <Image source={require('image!background')} style={styles.bgImage} />
  </View>
  <Text> // yours code
    Welcome to React Native!
  </Text>
</View>

对于按钮,您还可以使用 flexbox、填充、边距,因为应用在真实设备上的分辨率会有所不同。

关于ios - 样式 react native 组件 - 当以像素定义时,一切都太大了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41572267/

相关文章:

ios - Swift 3 - 覆盖 UINavigationController 的初始值设定项以设置 rootviewcontroller

IOS MDM 服务器 IOS App OTA 部署

objective-c - UICollectionView 列网格间隙

css - 如果不适合,防止 div 跳转到下一行

php - 用于匹配 CSS 媒体查询的递归/子例程正则表达式

javascript - react native 和 socket.io Node 不起作用

ios - 使用特定标识符设置所有单元格的高度?

javascript - react native WebView : Load html and js from assets folder

javascript - ComponentDidCatch 不起作用

html - 使用变换禁用缩放/旋转动画