【问题标题】:React Native styling header with Background Image用背景图像反应原生样式标题
【发布时间】:2020-07-06 15:48:04
【问题描述】:

我需要一些帮助。我正在尝试使用背景中的图像来设置屏幕标题的样式。但是背景图形的样式不正确,我尝试过同时使用ImageImageBackground。图片应适合宽度并位于背景中。

它应该是这样的:

这就是现在的样子:

当我将宽度设置为 100% 或窗口的宽度时,这就是我得到的,图像从底部被裁剪:

这是我的代码:

ArtistProfile.tsx

import React, { Component } from "react";
import { Content } from "native-base";
import styled from "styled-components/native";
import ScreenLayout from "../layout/ScreenLayout";

interface ArtistProfileProps {
  componentId: string;
}

class ArtistProfile extends Component<ArtistProfileProps> {
  render() {
    return (
      <ScreenLayout componentId={this.props.componentId}>
        <ArtistProfileContent>
          <HeaderBackground
            source={require("../../assets/img/header-bg.png")}
          />
        </ArtistProfileContent>
      </ScreenLayout>
    );
  }
}
export default ArtistProfile;

const ArtistProfileContent = styled(Content)`
  flex: 1;
`;

const HeaderBackground = styled.Image`
  flex: 1;
  align-self: center;
  margin: 0;
  padding: 0;
`;

ScreenLayout.tsx

import React, { Component } from "react";
import theme from "../../theme/Theme";
import styled, { ThemeProvider } from "styled-components/native";
import { Container } from "native-base";
import FooterNavigation from "../../components/footer/Footer";

interface ScreenLayoutProps {
  componentId: string;
}

class ScreenLayout extends Component<ScreenLayoutProps> {
  render() {
    return (
      <ThemeProvider theme={theme}>
        <ScreenLayoutContainer>{this.props.children}</ScreenLayoutContainer>
        <FooterNavigation componentId={this.props.componentId} />
      </ThemeProvider>
    );
  }
}
export default ScreenLayout;

const ScreenLayoutContainer = styled(Container)`
  flex: 1;
`;

【问题讨论】:

  • 尝试把 width: WINDOW.width, height: WINDOW.height 放到你的图片 css 中
  • @UgurYilmaz 是 React Native 我还能访问 WINDOW 吗?
  • 它不适用于窗口

标签: css reactjs react-native flexbox styled-components


【解决方案1】:

也许你可以这样设置宽度

import {Dimensions} from 'react-native';
const windowWidth = Dimensions.get('window').width;

react native dimensions

【讨论】:

  • 如果它在你的 ArtistProfileContent 中不起作用,也许你应该找到父标签的样式来找出外面的限制。尝试在不同的标签中添加背景颜色(使用不同的颜色就可以了),你会看到问题。如果 ScreenLayout 中的背景是屏幕的全宽,则问题必须在 ArtistProfile 中,或者在 ScreenLayout 中。类似的东西
  • 感谢您的回答,我已经尝试使用尺寸为全宽但底部波浪被裁剪。是的,ScreenLayout 是全宽的
  • 底部波浪被裁剪>>也许你可以在底部添加一点边距或填充来修复它?
  • 在样式中添加 paddingbuttom:15 或 marginbuttom:15 试试?
【解决方案2】:

您可以在 HeaderBackground 组件中将宽度设置为“100%”。

【讨论】:

  • 当我将宽度设置为 100% 或窗口宽度时,图像会从底部裁剪。而且这张图片应该在背景中,这样我就可以在上面放置组件。
  • 尝试在您的模拟器或应用程序中使用检查器元素,以便您可以轻松找到出现差距的位置。这是一个很好的做法,可以查看有关不必要的边距和间距的 UI 问题。 @MusayyabNaveed
  • snack.expo.io/H1QhAPcUL 看看这个关于零食的博览会例子。只需查看和按钮即可轻松完成。也请查看样式。
猜你喜欢
  • 2018-05-04
  • 1970-01-01
  • 1970-01-01
  • 2020-03-28
  • 1970-01-01
  • 2017-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多