react-native - react native : Cannot vertically align an image to bottom of the screen

标签 react-native

我正在尝试将大图像与屏幕底部对齐。应该注意的是,图像的宽度(远)大于屏幕的宽度。我的代码如下:

App.js

<View style={styles.container}>
    <View style={styles.imageContainer}>
        <Image
            source={LargeImage}
            style={styles.largeImage}
            resizeMode='contain'
        />
    </View>
</View>

Styles.js

const styles = Stylesheet.create({
    container: {
        flex: 1,
        backgroundColor: 'red'
    },
    imageContainer: {
        width: '100%'
        height: '70%',
        position: 'absolute',
        bottom: 0,
        backgroundColor: 'cyan'
    },
    largeImage: {
        width: '100%',
        height: '100%',
        position: 'absolute'
        bottom: 0
    } 
});

上面的代码呈现为: mobile app screenshot

其中红色是最上面的容器背景,浅蓝色/青色是图像容器背景,顶部弯曲的深蓝色形状是大图像。由于某种原因,图像在图像容器内居中,尽管我添加了绝对定位(删除“largeImage” block 中的绝对定位根本不会改变输出)。

如何对齐此图像,使其与屏幕底部齐平?我正在物理 Galaxy S9 Android 设备上运行此程序,运行最新版本的react-native。理想情况下,我想使用绝对定位来完成此任务。

最佳答案

想通了。解决方案是根据当前屏幕的物理尺寸计算缩放后的宽度和高度。样式代码如下:

const imageAspectRatio = 1080 / 872;
const scaledWidth = Dimensions.get('window').width;
const scaledHeight = scaledWidth / imageAspectRatio;

const styles = Stylesheet.create({
    container: {
        flex: 1,
        backgroundColor: 'red'
    },
    imageContainer: {
        width: '100%'
        height: '70%',
        position: 'absolute',
        bottom: 0,
        backgroundColor: 'cyan'
    },
    largeImage: {
        width: scaledWidth,
        height: scaledHeight,
        position: 'absolute'
        bottom: 0
    } 
});

关于react-native - react native : Cannot vertically align an image to bottom of the screen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57385815/

相关文章:

react-native - React native - ListView行中的绑定(bind)事件

reactjs - 在 React-Native 中更改方向后如何获取当前窗口高度和宽度?

javascript - 使用 React Native Router Flux 的合成场景栈(历史)

objective-c - 在 Swift 中实现 RCTEventEmitter 但未设置桥接异常

android - React Native 任​​务 ':react-native-reanimated:androidJavadoc' 执行失败

javascript - 读取父状态的值并将 bool 值传递给子组件React

javascript - 如何在模式中显示pdf而不是在react js中的新窗口中打开它

javascript - React Native Redux - 将变量传递到调度中

react-native - React Native - 如何自动将 ScrollView 滚动到光标位置?

android - 后台定期任务和通知显示