我在Android模拟器上的ReactNative中显示自定义组件时遇到问题。具体来说,它显示为一条扁平线(高度接近0 - 见“认证”正下方的蓝线),当它应该有一些高度。如何使按钮具有高度?见下文。谢谢!
这是我的代码:
App.js(与显示按钮相关的部分)
renderContent() {
return (
<Button onPress={() => firebase.auth().signOut()}>
Log Out
</Button>
);
}
render() {
return (
<View>
<Header headerText="Authentication"/>
{this.renderContent()} //button is rendered here.
</View>
);
}
自定义按钮:
import React from 'react';
import { Text, TouchableOpacity } from 'react-native';
const Button = ({ onPress, children }) => {
const { buttonStyle, textStyle } = styles;
return (
<TouchableOpacity onPress={onPress} style={buttonStyle}>
<Text style={textStyle}>
{children}
</Text>
</TouchableOpacity>
);
};
const styles = {
textStyle: {
alignSelf: 'center',
color: '#007aff',
fontSize: 16,
fontWeight: '600',
paddingTop: 10,
paddingBottom: 10
},
buttonStyle: {
flex: 1,
alignSelf: 'stretch',
backgroundColor: '#fff',
borderRadius: 5,
borderWidth: 1,
borderColor: '#007aff',
marginLeft: 5,
marginRight: 5
}
};
export { Button };
答案 0 :(得分:2)
您应指定自定义按钮高度。
<View>
<View>
<Header headerText="Authentication"/>
</View>
<View style={{height:50}}>
{this.renderContent()} //button is rendered here.
</View>
</View>
或者您可以使用ScrollView。
<View>
<View>
<Header headerText="Authentication"/>
</View>
<ScrollView>
{this.renderContent()} //button is rendered here.
</ScrollView>
</View>