如何使用android的styles.xml样式来设置React Native Picker的样式?

10

我想了解如何设置字体。如何在Picker.items上设置颜色和背景颜色?

<Picker
   style={styles.picker} // cannot set fontFamily here
   selectedValue={this.state.selected2}
   onValueChange={this.onValueChange.bind(this, 'selected2')}
   mode="dropdown">
   <Item label="hello" value="key0" /> // cannot set backgroundColor here
   <Item label="world" value="key1" />
</Picker>

我在 Github 上发帖了,得到的答复是 React Native 中只有一些属性可以通过样式进行设置,而选择器的一些更核心的元素(比如使用的字体)是由本地 Android 驱动的。需要通过使用 styles.xml 等本地 Android 方法来完成。

不幸的是,我不是本地 Android 开发人员,所以很难理解解决方案。我将以下代码片段添加到 /res/values/styles.xml 中,但弹出窗口的文本颜色或背景没有改变。

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
    </style>
    <style name="SpinnerItem">
        <item name="android:textColor">#ffffff</item>
        <item name="android:background">#993399</item>
    </style>
    <style name="SpinnerDropDownItem">
        <item name="android:textColor">#ffffff</item>
        <item name="android:background">#993399</item>
    </style>
</resources>

我需要做哪些其他更改?如何设置字体?

1个回答

17

那是因为你需要在你的AppTheme中声明样式

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="android:spinnerItemStyle">@style/SpinnerItem</item>
        <item name="android:spinnerDropDownItemStyle">@style/SpinnerDropDownItem</item>
    </style>
    <style name="SpinnerItem">
        <item name="android:textColor">#ffffff</item>
        <item name="android:background">#993399</item>
    </style>
    <style name="SpinnerDropDownItem">
        <item name="android:textColor">#ffffff</item>
        <item name="android:background">#993399</item>
    </style>
</resources>

就这样了 :)


谢谢。如果有人感兴趣,我已经在这里发布了我的代码:https://dev59.com/rlkT5IYBdhLWcg3wNs3-#39141949 - Abhishek Nalin
我该如何将模式设置为Spinner?React Native没有提供访问权限,我想使用Spinner而不是日历。 - Cobolt
4
使用Expo的话,有什么方法可以访问这个吗? - iuliu.net
这个能动态设置吗?我想为我的应用程序添加一个深色模式。 - Gutyn
我该如何更改textAlign? - Oliver D

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接