使用 react-native 设置选择器的高度
Set height of picker with react-native
我的 react-native ios 应用程序中有一个选择器,我想设置高度,但该示例不支持 flex、样式高度或高度属性。
<Picker
style={[styles.testbox, {borderColor: '#00F', flex:1, height: 100}]}
selectedValue={this.state.language}
height={100}
onValueChange={(itemValue, itemIndex) => this.setState({language: itemValue})}>
<Picker.Item label="Java" value="java" />
<Picker.Item label="JavaScript" value="js" />
</Picker>
文档中没有 height
道具,因此您可以删除它。
从玩弄样式来看,最重要的部分似乎是设置 itemStyle
属性并在那里定义 height
值。您可能还想设置 Picker
组件本身的样式并将 height
值设置为相同以获得最佳外观效果,但您 不需要 这样做。
最小示例:
<Picker style={{width: 200, height: 44}} itemStyle={{height: 44}}>
<Picker.Item label="Java" value="java" />
<Picker.Item label="JavaScript" value="js" />
</Picker>
这里是 Snack 显示了不同高度的完整示例(下面粘贴了代码副本):
import React, { Component } from 'react';
import { Text, View, StyleSheet, Picker } from 'react-native';
export default class App extends Component {
constructor(props) {
super(props)
this.state = {
language: 'haxe',
firstLanguage: 'java',
secondLanguage: 'js',
}
}
render() {
return (
<View style={styles.container}>
<Text style={styles.title}>Unstyled:</Text>
<Picker
style={[styles.picker]} itemStyle={styles.pickerItem}
selectedValue={this.state.language}
onValueChange={(itemValue) => this.setState({language: itemValue})}
>
<Picker.Item label="Java" value="java" />
<Picker.Item label="JavaScript" value="js" />
<Picker.Item label="Python" value="python" />
<Picker.Item label="Haxe" value="haxe" />
</Picker>
<Text style={styles.title}>Shows one row:</Text>
<Picker
style={[styles.onePicker]} itemStyle={styles.onePickerItem}
selectedValue={this.state.firstLanguage}
onValueChange={(itemValue) => this.setState({firstLanguage: itemValue})}
>
<Picker.Item label="Java" value="java" />
<Picker.Item label="JavaScript" value="js" />
<Picker.Item label="Python" value="python" />
<Picker.Item label="Haxe" value="haxe" />
</Picker>
<Text style={styles.title}>Shows above and below values:</Text>
<Picker
style={[styles.twoPickers]} itemStyle={styles.twoPickerItems}
selectedValue={this.state.secondLanguage}
onValueChange={(itemValue) => this.setState({secondLanguage: itemValue})}
>
<Picker.Item label="Java" value="java" />
<Picker.Item label="JavaScript" value="js" />
<Picker.Item label="Python" value="python" />
<Picker.Item label="Haxe" value="haxe" />
</Picker>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
alignItems: 'center',
padding: 20,
backgroundColor: 'white',
},
title: {
fontSize: 18,
fontWeight: 'bold',
marginTop: 20,
marginBottom: 10,
},
picker: {
width: 200,
backgroundColor: '#FFF0E0',
borderColor: 'black',
borderWidth: 1,
},
pickerItem: {
color: 'red'
},
onePicker: {
width: 200,
height: 44,
backgroundColor: '#FFF0E0',
borderColor: 'black',
borderWidth: 1,
},
onePickerItem: {
height: 44,
color: 'red'
},
twoPickers: {
width: 200,
height: 88,
backgroundColor: '#FFF0E0',
borderColor: 'black',
borderWidth: 1,
},
twoPickerItems: {
height: 88,
color: 'red'
},
});
我的 react-native ios 应用程序中有一个选择器,我想设置高度,但该示例不支持 flex、样式高度或高度属性。
<Picker
style={[styles.testbox, {borderColor: '#00F', flex:1, height: 100}]}
selectedValue={this.state.language}
height={100}
onValueChange={(itemValue, itemIndex) => this.setState({language: itemValue})}>
<Picker.Item label="Java" value="java" />
<Picker.Item label="JavaScript" value="js" />
</Picker>
文档中没有 height
道具,因此您可以删除它。
从玩弄样式来看,最重要的部分似乎是设置 itemStyle
属性并在那里定义 height
值。您可能还想设置 Picker
组件本身的样式并将 height
值设置为相同以获得最佳外观效果,但您 不需要 这样做。
最小示例:
<Picker style={{width: 200, height: 44}} itemStyle={{height: 44}}>
<Picker.Item label="Java" value="java" />
<Picker.Item label="JavaScript" value="js" />
</Picker>
这里是 Snack 显示了不同高度的完整示例(下面粘贴了代码副本):
import React, { Component } from 'react';
import { Text, View, StyleSheet, Picker } from 'react-native';
export default class App extends Component {
constructor(props) {
super(props)
this.state = {
language: 'haxe',
firstLanguage: 'java',
secondLanguage: 'js',
}
}
render() {
return (
<View style={styles.container}>
<Text style={styles.title}>Unstyled:</Text>
<Picker
style={[styles.picker]} itemStyle={styles.pickerItem}
selectedValue={this.state.language}
onValueChange={(itemValue) => this.setState({language: itemValue})}
>
<Picker.Item label="Java" value="java" />
<Picker.Item label="JavaScript" value="js" />
<Picker.Item label="Python" value="python" />
<Picker.Item label="Haxe" value="haxe" />
</Picker>
<Text style={styles.title}>Shows one row:</Text>
<Picker
style={[styles.onePicker]} itemStyle={styles.onePickerItem}
selectedValue={this.state.firstLanguage}
onValueChange={(itemValue) => this.setState({firstLanguage: itemValue})}
>
<Picker.Item label="Java" value="java" />
<Picker.Item label="JavaScript" value="js" />
<Picker.Item label="Python" value="python" />
<Picker.Item label="Haxe" value="haxe" />
</Picker>
<Text style={styles.title}>Shows above and below values:</Text>
<Picker
style={[styles.twoPickers]} itemStyle={styles.twoPickerItems}
selectedValue={this.state.secondLanguage}
onValueChange={(itemValue) => this.setState({secondLanguage: itemValue})}
>
<Picker.Item label="Java" value="java" />
<Picker.Item label="JavaScript" value="js" />
<Picker.Item label="Python" value="python" />
<Picker.Item label="Haxe" value="haxe" />
</Picker>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
alignItems: 'center',
padding: 20,
backgroundColor: 'white',
},
title: {
fontSize: 18,
fontWeight: 'bold',
marginTop: 20,
marginBottom: 10,
},
picker: {
width: 200,
backgroundColor: '#FFF0E0',
borderColor: 'black',
borderWidth: 1,
},
pickerItem: {
color: 'red'
},
onePicker: {
width: 200,
height: 44,
backgroundColor: '#FFF0E0',
borderColor: 'black',
borderWidth: 1,
},
onePickerItem: {
height: 44,
color: 'red'
},
twoPickers: {
width: 200,
height: 88,
backgroundColor: '#FFF0E0',
borderColor: 'black',
borderWidth: 1,
},
twoPickerItems: {
height: 88,
color: 'red'
},
});