DrawerNavigator: Change Text Color

佐手、 提交于 2020-11-25 02:04:19

问题


On react-navigation's DrawerNavigator, is there a way to change the text color and background color for the item?

By default, the color scheme looks like the following:

Which is initialized by the following:

export const Drawer = DrawerNavigator({
    dOne: {
      screen: Screen1,
    },
    dTwo: {
      screen: Screen2,
    }
  }
);

Setting the color property within the screen's contentOptions's style does not appear to have an effect.

Should I extend new components for each row (labeled "Primary", "Secondary", etc. right now)? or is there an easier way to stylize the existing implementation of the rows?


回答1:


Instead of style you need to use labelStyle inside contentOptions. Like this:

contentOptions: {
  labelStyle: {
    fontFamily: 'SomeFont',
    color: 'white',
  },
}



回答2:


A google search landed me here but the answer could not resolve my issue: I wanted to change the text color but have it switch between different colors depending on whether active and or inactive. Chnangin the colorin labelstyle effectively ensured the labels are the same color irrespective of the state. So I used the tint colors in content options of the drawer.

    export const DrawerStack = DrawerNavigator(
  {... /drawer items}
 ,
  {
    contentOptions: {
      activeTintColor: colors.primary,
      activeBackgroundColor: 'transparent',
      inactiveTintColor: 'black',
      inactiveBackgroundColor: 'transparent',
      labelStyle: {
        fontSize: 15,
        marginLeft: 10,
      },
    },
    drawerWidth: SCREEN_WIDTH * 0.7,
    drawerOpenRoute: 'DrawerOpen',
    drawerCloseRoute: 'DrawerClose',
    drawerToggleRoute: 'DrawerToggle',
  }
);

This way I can alternate colors depending on whether a drawer item is active or not using the activeTintColor and inactiveTintColor options.




回答3:


For anyone who is using Navigation V5 you will have to follow the below method and do that when initializing the drawer navigator

      <Drawer.Navigator
        drawerContentOptions={{
          activeTintColor: 'red',
          activeBackgroundColor: 'grey',
          inactiveTintColor: 'blue',
          inactiveBackgroundColor: 'white',
          labelStyle:{
            marginLeft:5
          }
        }}>
  </Drawer.Navigator>

You can refer the docs for more props https://reactnavigation.org/docs/drawer-navigator/#drawercontentoptions



来源:https://stackoverflow.com/questions/44930525/drawernavigator-change-text-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!