1

我的 React Native 应用程序中有以下屏幕: 在此处输入图像描述

蓝黑白底是ImageBackgrounda,中间的绿条是a View。代码是:

<ImageBackground
  source={require('@images/login-background.jpg')}
  style={{
    flex: 1,
    width: '100%',
    resizeMode: 'cover',
    justifyContent: 'center',
  }}
>
  <View style={{
    width: '100%',
    backgroundColor: 'green',
    borderRadius: 15,
    padding: 15
  }}/>
  </ImageBackground>

我希望15px在 green 的左右边缘有填充View。如果是ImageBackgroundView我会在其中添加15px填充,但是当它是时ImageBackground,会导致:

在此处输入图像描述

相反,如果我添加margin: 15px到 green View,我会得到:

在此处输入图像描述

我应该如何处理这个,让它看起来像这样?

在此处输入图像描述

4

2 回答 2

2

Dimensions您可以使用React-Native实现上述要求

import React from "react";
import { ImageBackground, View, Dimensions } from "react-native";

const screenWidth = Dimensions.get('window').width;

export default App = () => (
    <ImageBackground
        source={{ uri: "https://reactjs.org/logo-og.png" }}
        style={{
            flex: 1,
            width: '100%',
            resizeMode: 'cover',
            justifyContent: 'center',
            alignItems: 'center'
        }}>
        <View style={{
            width: screenWidth - 30,
            backgroundColor: 'green',
            borderRadius: 15,
            padding: 15
        }} />
    </ImageBackground>
);

希望这对您有所帮助。随意怀疑。

于 2020-04-24T02:48:12.803 回答
2

试试这个:

    <ImageBackground
      source={image}
      style={{
        flex: 1,
        width: '100%',
        resizeMode: 'cover',
        justifyContent: 'center',
      }}
    >
    <View style={{paddingHorizontal: 15}}>
      <View style={{
       width: '100%',
       backgroundColor: 'green',
       borderRadius: 15,
       padding: 15,
      }}/>
    </View>
    </ImageBackground>
于 2020-04-24T02:52:37.410 回答