简体   繁体   English

React Native 中的“react-native-vector-icons”问题

[英]Problem with "react-native-vector-icons" in React native

I install this library :我安装这个库:

npm install react-native-vector-icons

and link it并链接它

react native link

and i import it to my project我将它导入到我的项目中

import Icon from 'react-native-vector-icons/Ionicons'

then i use it in render part of the component like this :然后我在组件的渲染部分中使用它,如下所示:

<Icon name={'ios-person-outline'} />

But when i run the android app there is no error but the icon is a qustion icon eny body has idea ?但是,当我运行 android 应用程序时,没有错误,但图标是一个问题图标,您知道吗?

I used to have similar issues when I started with react-native-vector-icons until I got to understand the different type components.当我开始使用 react-native-vector-icons 直到我了解不同类型的组件时,我曾经遇到过类似的问题。

type="AntDesign"

type="Entypo"

type="FontAwesome"

type="FontAwesome5"

type="FontAwesome5Brands"

type="Foundation"

type="Ionicons"

type="MaterialCommunityIcons"

type="MaterialIcons"

type="SimpleLineIcons"

type="Octicons"

etc...等等...

Icon name "person-outline" can be found under "MaterialIcons" on "https://oblador.github.io/react-native-vector-icons/", so we can do;图标名称“person-outline”可以在“https://oblador.github.io/react-native-vector-icons/”的“MaterialIcons”下找到,所以我们可以这样做;

<Icon name='person-outline' type="MaterialIcons" />

Hope this explanation helps.希望这个解释有帮助。

NOTE: Make sure you take the initial steps like;注意:确保您采取初始步骤;

npm install react-native-vector-icons --save

AND

react-native link react-native-vector-icons

react-native-vector-icons have a lot of 'types' for the icons, you can see them here: https://oblador.github.io/react-native-vector-icons/ react-native-vector-icons有很多图标的“类型”,你可以在这里看到它们: https : //oblador.github.io/react-native-vector-icons/

You should specify the type of the icon on your import or when you are using them in your code.您应该在导入时或在代码中使用它们时指定图标的类型。 For example:例如:

import SimpleIcon from 'react-native-vector-icons/SimpleLineIcons';

The code above will import only the SimpleLineIcons, then you can use them like this:上面的代码将只导入 SimpleLineIcons,然后你可以像这样使用它们:

<SimpleIcon name="user" color="rgba(110, 120, 170, 1)" size={25} />

Or, you can import them like this:或者,您可以像这样导入它们:

import Icon from 'react-native-vector-icons/'

And then, use it like this (specifying the type):然后,像这样使用它(指定类型):

<Icon
  name='check'
  color='rgba(0, 0, 0, 0.38)'
  size={25}
  type="entypo"
/>

In addition to @Kivul's answer.除了@Kivul 的回答。 Whenever you want to use the react-native-vector-icons.每当您想使用 react-native-vector-icons 时。 Instead of specifying the type on the component, just simply import the type of icon the name belongs to and use it.无需在组件上指定类型,只需导入名称所属的图标类型并使用它即可。 Below is an example of this:下面是一个例子:

import { 
   Ionicons, 
   Entypo, 
   MaterialCommunityIcons, 
   FontAwesome 
} from '@expo/vector-icons/';

Then in your render method, return any of the components, and pass in the name of the icon.然后在您的渲染方法中,返回任何组件,并传入图标的名称。

pick the name from https://oblador.github.io/react-native-vector-icons/从 https://oblador.github.io/react-native-vector-icons/ 选择名称

<FontAwesome 
  name={'money'} 
 ... 
 />

Same with the others, find the name, then the component it belongs to, import it and add the name to it.和其他一样,找到名称,然后它所属的组件,导入它并添加名称。

I hope this helps?我希望这有帮助?

There can be two things, either you might not be writing the correct name of the icon or that specific icon is not being found via the library.可能有两件事,要么您可能没有写出正确的图标名称,要么未通过库找到该特定图标。 Its a very common problem with the library.这是图书馆的一个非常普遍的问题。 Try changing the icon name with some other more common icon and it will work.尝试使用其他一些更常见的图标更改图标名称,它会起作用。 Its not actually issue.它实际上不是问题。 Please check and see if that helps.请检查,看看是否有帮助。 Thanks.谢谢。

Also you should change the import statement of Ionicons to /Ionicons .此外,您应该将 Ionicons 的 import 语句更改为/Ionicons I am not sure how is that with \\ even working but hope my solution helps.我不确定\\即使工作如何,但希望我的解决方案有所帮助。

Have just tried to use the ios-person-outline , and as expected it didn't work, but ios-person did.刚刚尝试使用ios-person-outline ,正如预期的那样,它没有用,但是ios-person却用了。 Its a very common issue with the library that it doesnt honor the outline and other kinds of icons, it also happens with the material icon when trying to use solid or some other type of icon.库的一个非常常见的问题是它不支持轮廓和其他类型的图标,当尝试使用实体或其他类型的图标时,材质图标也会发生这种情况。

Step 1: Install react-native-element:第 1 步:安装 react-native-element:

npm install react-native-elements --save

Steps 2: Install react-native-vector-icons Install from npm步骤 2:安装 react-native-vector-icons 从 npm 安装

npm install react-native-vector-icons --save

Link It链接它

react-native link react-native-vector-icons

Step 1: After that you can use it on your page by第 1 步:之后,您可以通过以下方式在您的页面上使用它

import { Icon } from 'react-native-elements';

Step 2:第2步:

<Icon name="md-beer" type="ionicon" color="#887700" />

尝试将下一行添加到您的 /app/build.gradle

apply from: "../../node_modules/react-native-vector-icons/fonts.gradle"

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

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