如何在RN Picker android中心项目和所选项目?

时间:2016-05-09 14:51:32

标签: react-native

是否可以将android选取器项目和所选项目居中?我到处搜索,尝试使用alignItems:'center'& justifyContent:'center',但是当我放置alignSelf时,项目仍然左对齐(在照片上可见):'center'选择器不再可见。有人有任何线索吗?

    var REPORTS = [
        {name: 'Report 1', id: 'r1'},
        {name: 'Report 2', id: 'r2'},
        {name: 'Report 3', id: 'r3'},
        {name: 'Report 4', id: 'r4'},
        {name: 'Report5', id: 'r5'},
        {name: 'Report6', id: 'r6'}
    ];

    ...
        <Picker style={styles.androidPicker} mode={'dropdown'}
                selectedValue={this.state.report} itemStyle={styles.reports.iosPicker}
                onValueChange={(reportId) => this.onReportChanged(reportId)}>
           {REPORTS.map(function (reports) { 
               return <Picker.Item style={{alignSelf:'center'}} label={reports.name} 
                           value={reports.id} key={reports.name + reports.id}/>;})} </Picker>

    ...

样式:

var styles = StyleSheet.create({
    androidPicker: {
            flex: 1,
            color: '#6D6D6D',
            backgroundColor: '#FFF',
            marginBottom: 20,
            height: 40, 
            alignItems:'center', 
            justifyContent:'center',
            flexDirection: 'row'
        }
})

以下是已关闭和已打开的选择器的快照:

RN Android Picker closed RN Android Picker opened

提前致谢! :)

3 个答案:

答案 0 :(得分:0)

你会尝试如下:

public static class VisualTreeHelperExtensions
{
    public static T FindParent<T>(this DependencyObject child) where T : DependencyObject
    {
        while (true)
        {
            //get parent item
            DependencyObject parentObject = VisualTreeHelper.GetParent(child);

            //we've reached the end of the tree
            if (parentObject == null) return null;

            //check if the parent matches the type we're looking for
            T parent = parentObject as T;
            if (parent != null)
                return parent;
            child = parentObject;
        }
    }
}

答案 1 :(得分:0)

尝试:

androidPicker: { color: '#6D6D6D', backgroundColor: '#FFF', marginBottom: 20, height: 40, alignSelf: 'center', justifyContent:'center', }

对我有用

答案 2 :(得分:0)

首先你需要设置useNativeAndroidPickerStyle={false},因为这将允许你使用一些样式道具,如inputAndroid和inputAndroidContainer等。然后你可以在这些样式中进行必要的样式设置。查看文档{{ 3}},你就会明白。