我对React Native还是很陌生,我现在正在尝试一些东西。我还获得了第一个演示应用程序,运行顺利。
现在,我想测试教程中的示例,并在右上角添加带有窗口标题和按钮的标题栏。为此,我扩展了MyNewProject。但是,如果我现在启动该应用程序,则顶部不会显示任何栏,仅显示窗口中间的文本。我在做什么错了?
这是程序代码:
const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
android:
'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu',
});
type Props = {};
export default class App extends Component<Props> {
static navigationOptions = {
title: 'My new project',
headerRight: (
<Button
onPress={() => Alert.alert('This is a button!')}
title='Info'
/>
),
};
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>Welcome to React Native!</Text>
<Text style={styles.instructions}>To get started, edit App.js</Text>
<Text style={styles.instructions}>{instructions}</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
答案 0 :(得分:0)
尝试更改
static navigationOptions = {
title: 'My new project',
headerRight: (
<Button
onPress={() => Alert.alert('This is a button!')}
title='Info'
/>
),
};
对此:
static navigationOptions = props => { return {
title: 'My new project',
headerRight: (
<Button
onPress={() => Alert.alert('This is a button!')}
title='Info'
/>
),
}
};