【问题标题】:Why are the buttons not clickable when ImageBackground is used?为什么使用 ImageBackground 时按钮无法点击?
【发布时间】:2023-03-21 22:15:02
【问题描述】:

我为我的 React Native 应用程序实现了 ImageBackground,当我运行模拟器时,它不允许按下按钮。按钮事先经过测试并正常运行。 我觉得这与组件的订购有关,但在重新订购时没有成功。

export function TitleScreen({navigation: navigation}) {
    return (
        <ImageBackground source={require('../assets/space_background_reduced_v1.png')} style={globalStyles.background}>
            <Image source = {require('../assets/logo.png')} style={globalStyles.logo}/>
            <View style={globalStyles.Buttons}>

                <Button title={"Random Race"} onPress={() => navigation.navigate('RandomRaceOptionsScreen')}/>
                <Button title={"Create"} onPress={() => navigation.navigate('CreateMenu')}/>
                <Button title={"Race Lore"} onPress={() => navigation.navigate('RaceLoreListScreen')}/>


            </View>

        </ImageBackground>
    );
}

样式:

export const globalStyles = StyleSheet.create({

    Buttons: {
        zIndex:5,
        width: 120,
        marginLeft: "33%",
        marginTop:90
    },
``

【问题讨论】:

  • 您还需要与我们分享样式。样式破坏了按钮的可按下功能。
  • 道歉,现在分享
  • 其实你也需要分享background风格。我正在为你准备一个例子。稍等一下。

标签: android typescript react-native


【解决方案1】:

这是ButtonImageBackground 中的工作示例。

import React from 'react';
import {View, Image, Button, StatusBar, ImageBackground} from 'react-native';

const App = () => {
  return (
    <>
      <StatusBar barStyle="dark-content" />
      <ImageBackground
        source={require('./assets/ahmed-yaaniu-05A3CzImkhw-unsplash.jpg')}
        style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
        <Image
          source={require('./assets/apple-logo.png')}
          style={{height: 50, width: 50}}
        />
        <View
          style={{
            width: 120,
            marginTop: 90,
          }}>
          <Button title={'Random Race'} onPress={() => {}} />
          <Button title={'Create'} onPress={() => {}} />
          <Button title={'Race Lore'} onPress={() => {}} />
        </View>
      </ImageBackground>
    </>
  );
};

export default App;

不要直接设置样式。首先,检查此示例并在此基础上重新设计。

另外,我建议你使用React Navigation Helpers 来避免navigation 钻孔。我是这篇文章的作者,它适用于 React Navigation v4 和 v5。

【讨论】:

  • 谢谢你,我已经尝试按照这个实现它,但仍然无法访问按钮。
  • 你能把完整的代码发给我吗?我可以快速调查它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-27
  • 2021-12-05
  • 1970-01-01
  • 1970-01-01
  • 2020-08-23
相关资源
最近更新 更多