简体   繁体   English

样式 React Native Picker

[英]Styling React Native Picker

I'm attempting to style the text color of the items in a React Native picker.我正在尝试在 React Native 选择器中设置项目的文本颜色样式。 I've only been working in iOS so far, but if I could find a cross-platform solution that'd be awesome.到目前为止,我只在 iOS 上工作过,但如果我能找到一个跨平台的解决方案,那就太棒了。

I've tried the following things:我尝试了以下几件事:

Styling color on Picker Picker 上的样式颜色

<Picker style={{color:'white'}} ...etc >

Styling color on Picker Items选择器项目上的样式颜色

<Picker.Item style={{color:'#fff'}} label={'Toronto'} value={'Toronto'} />

I've also seen some examples of adding a color property, so I tried this我也看到了一些添加颜色属性的例子,所以我尝试了这个

<Picker.Item color='white' label={'Toronto'} value={'Toronto'} />

At a total loss here.在这里完全不知所措。 Thanks for any insight!感谢您的任何见解!

EDIT: Here's a solution - use itemStyle prop in the Picker element.编辑:这是一个解决方案 - 在 Picker 元素中使用 itemStyle 道具。 I believe this is iOS only.我相信这只是iOS。

<Picker itemStyle={{color:'white'}} >
      <Picker.Item color='white' label={'Toronto'} value={'Toronto'} />
      <Picker.Item  label={'Calgary'} value={'Calgary'} />
</Picker>

要更改颜色,您需要使用:

<Item label="blue" color="blue" value="blue" />

To change text color, background color, font size and other Misc stuff like margin and padding, you can use "itemStyle" like below:要更改文本颜色、背景颜色、字体大小和其他诸如边距和填充之类的其他内容,您可以使用“itemStyle”,如下所示:

<Picker
    selectedValue={this.state.selectedItem}
    style={{ height: 100, width: 200 }}
    onValueChange={this.onChangeItem}
    itemStyle={{ backgroundColor: "grey", color: "blue", fontFamily:"Ebrima", fontSize:17 }}
>
    {cityItems}
</Picker>

If you need a border around the picker, you can wrap the Picker inside a View and provide border to it.如果您需要选择器周围的边框,您可以将选择器包装在视图中并为其提供边框。 Like below:像下面这样:

<View style={{ borderWidth: 1, borderColor: 'red', borderRadius: 4 }}>
    <Picker
        selectedValue={this.state.selectedItem}
        style={{ height: 100, width: 200 }}
        onValueChange={this.onChangeItem}
        itemStyle={{ backgroundColor: "grey", color: "blue", fontFamily:"Ebrima", fontSize:17 }}
    >
        {cityItems}
    </Picker>
</View>

Happy coding :)快乐编码:)

As mentioned, the itemStyle is an advanced property only supported by iOS Platform , as you can see on React Native docs here .如前所述, itemStyle是仅受iOS 平台支持的高级property ,您可以在此处的 React Native 文档中看到 Therefore, for styling the Picker items on Android, like changing the fontFamily can be done only using Native Android styles , for now .因此,对于 Android 上的 Picker 项目的样式,例如更改fontFamily只能使用原生 Android 样式来完成,目前 Then it will work for both platforms.然后它将适用于两个平台。

With that in mind, you would add a new <style> tag with your resources to the style.xml file, usually localized in the path: android/app/src/main/res/values/styles.xml .考虑到这一点,您可以将带有资源的新<style>标记添加到style.xml文件中,通常在路径中本地化: android/app/src/main/res/values/styles.xml And set it on AppTheme to count with the style added, eg:并将其设置在AppTheme以计算添加的样式,例如:

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
     <item name="android:spinnerItemStyle">@style/SpinnerItem</item>
     <item name="android:spinnerDropDownItemStyle">@style/SpinnerDropDownItem</item>
    </style>

    <!-- Item selected font. -->
    <style name="SpinnerItem" parent="Theme.AppCompat.Light.NoActionBar">
     <item name="android:fontFamily">casual</item>
    </style>

    <!-- Dropdown options font. -->
    <style name="SpinnerDropDownItem" parent="Theme.AppCompat.Light.NoActionBar">
      <item name="android:fontFamily">casual</item>
      <item name="android:padding">8dp</item>
    </style>
<resources>

Important Note: Sometimes the style property name in Android you are looking for is different from the one used in React Native.重要提示:有时您在 Android 中寻找的样式属性名称与 React Native 中使用的不同。 Eg positioning you might need to use android:gravity instead of flex .例如定位,您可能需要使用android:gravity而不是flex Therefore, you may require to search the dedicated XML tag attribute on Android in order to style it.因此,您可能需要在 Android 上搜索专用的 XML 标记属性以对其进行样式设置。 With that in mind, translating in React Native color for Text fonts is android:textColor for Android.考虑到这一点,为文本字体翻译 React Native colorandroid:textColor for Android。

Useful links:有用的链接:

Simply add the picker inside TouchableOpacity and apply styles to the TouchableOpacity.只需在 TouchableOpacity 中添加选择器并将样式应用到 TouchableOpacity。
Example:-例子:-

<TouchableOpacity style={[style.textFieldSmall, style.textInput]}>
 <Picker
   style={{color: '#fff', placeholderTextColor: '#fff'}}
   selectedValue={this.state.choosenLabel}
   onValueChange={
   (itemValue, itemIndex) => this.setState({
    choosenLabel: itemValue, 
    choosenindex:itemIndex})
    }
 >
  <Picker.Item label="Purchase" color="white" value="Purchase" />
  <Picker.Item label="YES" value="YES" />
  <Picker.Item label="NO" value="NO" />
 </Picker>
 </TouchableOpacity>

wrap it inside a <View> ,将其包裹在<View>

 <View style={styles.card}>
 <Picker itemStyle={{color:'white'}} >
      <Picker.Item color='white' label={'Toronto'} value={'Toronto'} />
      <Picker.Item  label={'Calgary'} value={'Calgary'} />
</Picker>
</View>


//Styles
const styles = StyleSheet.create({
card:{
    borderWidth: 1,
    width: 314,
    borderColor: "rgba(155,155,155,1)",
    borderBottomLeftRadius: 10,
    backgroundColor: "rgba(214,210,210,1)",
    marginTop: 10,
    marginLeft: 4
  },
})

试试这个: https : //github.com/caoyongfeng0214/rn-overlay/wiki/Picker

itemTextStyle: { color: 'red' }

您应该尝试使用 backgroundColor 而不是颜色: https : //facebook.github.io/react-native/docs/view.html#style

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM