简体   繁体   English

React 本机矢量图标不会在 android 设备中显示

[英]react native vector icons won't show in android device

I used react-native-vector-icons in my react native project and start app with npm start .我在我的 react native 项目中使用了 react-native-vector-icons 并使用npm start启动应用程序。

Icons are displaying normally in iOS, but won't show in android.图标在iOS正常显示,android不显示。

Things I tried:我尝试过的事情:

  • three method in the doc of react-native-vector-icons (finally I can see *.tff files in the fonts folder) react-native-vector-icons 文档中的三种方法(最后我可以在 fonts 文件夹中看到 *.tff 文件)
  • use react-native run-android to start app.使用react-native run-android启动应用程序。 Icon shows normal but what I want is integrating react native with my existing android app, not a totally RN app.图标显示正常,但我想要的是将 React Native 与我现有的 android 应用程序集成,而不是完全 RN 应用程序。
  • use jsbundle file instead of debug server in my app在我的应用程序中使用 jsbundle 文件而不是调试服务器

None of above works以上均无效

So, should I add something to my existing android app?那么,我应该向我现有的 android 应用添加一些东西吗?

I don't know how to solve this problem我不知道如何解决这个问题


react@15.2.0

react-native@0.30.0

react-native-vector-icons@2.0.3

node v5.10.1

npm v3.8.3

Open android/app/build.gradle and add the following:打开android/app/build.gradle并添加以下内容:

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

You can follow the instructions to properly install the module on Android: react-native-vector-icons#install-android您可以按照说明在 Android 上正确安装模块: react-native-vector-icons#install-android

I fixed it this by executing:我通过执行解决了这个问题:

react-native link
react-native run-android

Open terminal on same project path :在同一项目路径上打开终端:

react-native link react-native-vector-icons 
react-native run-android 

Edit android/app/build.gradle编辑 android/app/build.gradle

Just add in dependencies只需添加依赖项

implementation project(':react-native-vector-icons')实施项目(':react-native-vector-icons')

Below lines of code are optional if you are using RN >0.60如果您使用 RN >0.60,以下代码行是可选的

Edit android/app/build.gradle ( NOT android/build.gradle ) and add the following:编辑android/app/build.gradle ( NOT android/build.gradle ) 并添加以下内容:

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

Edit android/settings.gradle to look like this:编辑android/settings.gradle看起来像这样:

rootProject.name = 'MyApp'

include ':app'

// Add these two lines
include ':react-native-vector-icons'
project(':react-native-vector-icons').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-vector-icons/android')

references: https://github.com/oblador/react-native-vector-icons参考: https ://github.com/oblador/react-native-vector-icons

After doing the below-Mentioned steps, working Fine.完成以下提到的步骤后,工作正常。

Before, make sure these steps.之前,请确保这些步骤。

Install package:安装包:

 yarn add react-native-vector-icons

Import: import Ionicons from 'react-native-vector-icons/Ionicons';导入: import Ionicons from 'react-native-vector-icons/Ionicons';

Example code:示例代码:

return (
      <View style={styles.buttonCircle}>
        <Ionicons
          testID="nextButton"
          name="arrow-forward"
          color="rgba(255, 255, 255, .9)"
          size={24}
          style={{backgroundColor: 'transparent'}}
        />
      </View>
    );

Open android/app/build.gradle and Add below mentioned line after the react.gradle .打开android/app/build.gradle并在react.gradle之后添加下面提到的行。

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

Open android/settings.gradle Add below Mentioned line.打开android/settings.gradle添加下面提到的行。

// Add these two lines // 添加这两行

include ':react-native-vector-icons'
project(':react-native-vector-icons').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-vector-icons/android')

You Stop the Development server and Rerun the App react-native run-android您停止开发服务器并重新运行应用程序react-native run-android

I Hope, It's helpful.我希望,这很有帮助。

I've same issue and than I solved this, let's try :我有同样的问题,比我解决了这个问题,让我们试试:

  1. Open android/app/build.gradle ( NOT android/build.gradle )打开android/app/build.gradle (不是android/build.gradle
  2. add the following: apply from: file("../../node_modules/react-native-vector-icons/fonts.gradle")添加以下内容: apply from: file("../../node_modules/react-native-vector-icons/fonts.gradle")
  3. After that you must stop the Dev, and Restart/Run it again( npm run android )之后,您必须停止开发,然后重新启动/运行它( npm run android

Hopefully this will help you :D希望这会对您有所帮助:D

In :-   android/app/build.gradle

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

在此处输入图像描述

and setting.gradle file add this 

include ':react-native-vector-icons'
project(':react-native-vector-icons').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-vector-icons/android')

在此处输入图像描述

I had everything configured as mentioned in other answers but still running react-native run-android i keep seeing the app without the icons!我已按照其他答案中的说明进行了所有配置,但仍在运行react-native run-android我一直看到没有图标的应用程序!

Simply i did:我只是做了:

cd android && ./gradlew clean

then another然后另一个

react-native run-android

And it worked yaay!奏效了!

Open android/app/bundle.gradle打开 android/app/bundle.gradle

在此处输入图像描述

I am having the same issue and I fixed it this by executing:我有同样的问题,我通过执行解决了这个问题:

npx react-native link react-native-vector-icons npx react-native run-android npx react-native 链接 react-native-vector-icons npx react-native run-android

在此处输入图像描述

when importing the Icon it show something like that.导入图标时,它会显示类似的内容。

To fix this run,要修复此运行,

npx react-native link && npx react-native run android

After the command execution命令执行后

在此处输入图像描述

Follow the official recommandations ( https://github.com/oblador/react-native-vector-icons#android ) to have this module loads when creating the bundle :按照官方建议( https://github.com/oblador/react-native-vector-icons#android )在创建包时加载此模块:

Edit android/app/build.gradle ( NOT android/build.gradle ) and add the following:编辑 android/app/build.gradle ( NOT android/build.gradle ) 并添加以下内容:

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

This may have several possible answers:这可能有几个可能的答案:

1- maybe the icon that you use is just for ios ...try to see other logo packs in link below: https://oblador.github.io/react-native-vector-icons 1-也许您使用的图标仅适用于 ios ...尝试在下面的链接中查看其他徽标包: https ://oblador.github.io/react-native-vector-icons

2- maybe you import the wrong link for using the package at the top of your code where you import this vector icon, import exact link for example: 2-也许您在导入此矢量图标的代码顶部导入了错误的链接以使用该包,例如导入确切的链接:

this is wrong: import Icon from "react-native-vector-icons and this is true: "import Icon from "react-native-vector-icons/MaterialIcons";"这是错误的: import Icon from "react-native-vector-icons "import Icon from "react-native-vector-icons/MaterialIcons";"

I followed every solution but nothing worked我遵循了每一个解决方案,但没有任何效果

  1. Link command (Link not supported)链接命令(不支持链接)
  2. asset command (config file not exists)资产命令(配置文件不存在)

Both didn't worked but at last solution that works is Adding following lines in build.gradle file两者均无效,但最后有效的解决方案是在 build.gradle 文件中添加以下行

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

Paht of the file is android/app/build.gradle该文件的路径是android/app/build.gradle

NOTE:- please use npx react-native run-android command again to see changes注意:- 请再次使用 npx react-native run-android 命令查看更改

In my case auto linking was failing.在我的例子中,自动链接失败了。 I resolved it by copying Fonts folder from react-native-vector-icons inside node_modules to src/main/assets/fonts.我通过将 Fonts 文件夹从 node_modules 中的 react-native-vector-icons 复制到 src/main/assets/fonts 来解决它。

simplely npm unlink react-native-vector-icons ,and then npm link react-native-vector-icons .简单npm unlink react-native-vector-icons ,然后npm link react-native-vector-icons (if your ReactNative version is below 0.60) (如果你的 ReactNative 版本低于 0.60)

In Android platform icon showed me like this:在 Android 平台图标显示我是这样的:

在此处输入图像描述

enter image description here在此处输入图像描述

after some time of troubleshouting I finally find out solution.经过一段时间的故障排除后,我终于找到了解决方案。

Beacouse of I use my own icon set I created iconSet.ttf from iconSet.svg by IcoMoon App.因为我使用了自己的图标集,所以我通过 IcoMoon App 从 iconSet.svg 创建了 iconSet.ttf。

In fact I followed this tutorial: https://medium.com/bam-tech/add-custom-icons-to-your-react-native-application-f039c244386c事实上,我遵循了本教程: https ://medium.com/bam-tech/add-custom-icons-to-your-react-native-application-f039c244386c

IcoMoon App created *.ttf and selection.json and problem was in selection.json. IcoMoon 应用程序创建了 *.ttf 和 selection.json,问题出在 selection.json 中。

I renamed *.ttf file to myIcons.ttf but I had to change fontFamily and name in the end of file selection.json to same name like I named *.ttf file.我将 *.ttf 文件重命名为 myIcons.ttf 但我必须将文件 selection.json 末尾的 fontFamily 和名称更改为与我命名 *.ttf 文件相同的名称。 In my case myIcons.ttf I renamed name and fontFamily to myIcons in selection.json在我的情况下 myIcons.ttf 我在 selection.json 中将 name 和 fontFamily 重命名为 myIcons

You can use icons as SVG just by using selection.json file.您只需使用selection.json文件即可将图标用作 SVG。 The Developer Experience is very high and requires very little effort.开发人员体验非常高,需要很少的努力。

I hope this module makes your work easier when working with icons.我希望这个模块能让你在使用图标时更轻松。

react-icomoon 反应-icomoon

npm install react-native-vector-icons@9.2.0 --save npm 安装 react-native-vector-icons@9.2.0 --save

This version worked for me !这个版本对我有用!

After adding the font names to my android/app/build.gradle it is fixed.将字体名称添加到我的android/app/build.gradle ,它已修复。

project.ext.vectoricons = [
    iconFontNames: [ 'Feather.ttf',"FontAwesome.ttf" ] // Name of the font files you want to copy
]

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

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