如何通过kivy向Android应用程序添加字体?

时间:2018-09-24 01:55:37

标签: android kivy

我已经通过此路径/data/data/package-name/files/app/filefolder/file.txt在android中添加了一些txt文件

txt文件与main.py之类的/same_directory_with_main.py/filefolder/file.txt位于同一目录

基于这种经验,我试图在我的应用程序中添加一些自定义字体。 并且我使目录与添加txt文件的方式完全相同。

main.py的一部分

from kivy.config import Config      
Config.set('kivy', 'default_font', [
    '/data/data/org.test.tubuc/files/app/font/NanumSquareR.ttfs',
    '/data/data/org.test.tubuc/files/app/font/NanumSquareL.ttfs',
])

,但是logcat说找不到字体路径。 这里是日志。

09-24 10:47:57.506 18392 18581 I python  : ('Android kivy bootstrap done. __name__ is', '__main__')
09-24 10:47:57.540 18392 18581 I python  : ['/data/user/0/org.test.tubuc/files/app/lib/python2.7/site-packages', '/data/user/0/org.test.tubuc/files/app/lib/site-python']
09-24 10:47:57.540 18392 18581 I python  : AND: Ran string
09-24 10:47:57.540 18392 18581 I python  : Run user program, change dir and execute entrypoint
09-24 10:47:57.650 18392 18581 I python  : [WARNING] [Config      ] Older configuration version detected (0 instead of 19)
09-24 10:47:57.650 18392 18581 I python  : [WARNING] [Config      ] Upgrading configuration in progress.
09-24 10:47:57.659 18392 18581 I python  : [INFO   ] [Logger      ] Record log in /data/user/0/org.test.tubuc/files/app/.kivy/logs/kivy_18-09-24_0.txt
09-24 10:47:57.659 18392 18581 I python  : [INFO   ] [Kivy        ] v1.10.0
09-24 10:47:57.660 18392 18581 I python  : [INFO   ] [Python      ] v2.7.2 (default, Aug 17 2018, 08:01:29) 
09-24 10:47:57.660 18392 18581 I python  : [GCC 4.9.x 20150123 (prerelease)]
09-24 10:47:57.691 18392 18581 I python  : [INFO   ] [Factory     ] 194 symbols loaded
09-24 10:47:57.994 18392 18581 I python  : [INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2, img_gif (img_pil, img_ffpyplayer ignored)
09-24 10:47:58.022 18392 18581 I python  : [INFO   ] [Text        ] Provider: sdl2
09-24 10:47:58.023 18392 18581 I python  :  Traceback (most recent call last):
09-24 10:47:58.023 18392 18581 I python  :    File "/root/Desktop/hi/.buildozer/android/app/main.py", line 14, in <module>
09-24 10:47:58.024 18392 18581 I python  :    File "/root/Desktop/hi/.buildozer/android/platform/build/dists/tubuc/private/lib/python2.7/site-packages/kivy/uix/button.py", line 40, in <module>
09-24 10:47:58.024 18392 18581 I python  :    File "/root/Desktop/hi/.buildozer/android/platform/build/dists/tubuc/private/lib/python2.7/site-packages/kivy/uix/label.py", line 246, in <module>
09-24 10:47:58.024 18392 18581 I python  :    File "/root/Desktop/hi/.buildozer/android/platform/build/dists/tubuc/private/lib/python2.7/site-packages/kivy/core/text/__init__.py", line 797, in <module>
09-24 10:47:58.025 18392 18581 I python  :    File "/root/Desktop/hi/.buildozer/android/platform/build/dists/tubuc/private/lib/python2.7/site-packages/kivy/core/text/__init__.py", line 248, in register
09-24 10:47:58.025 18392 18581 I python  :  IOError: File /data/data/org.test.tubuc/files/app/font/NanumSquareL.ttfs not found
09-24 10:47:58.042 18392 18581 I python  : Python for android ended.

如何为Android添加字体?谢谢。

1 个答案:

答案 0 :(得分:0)

这是我在与main.py相同级别上使用字体文件的方法

#main.py
import os
tools_path = os.path.dirname(__file__)
icons_path = os.path.join(tools_path, 'NanumSquareL.ttfs')

在buildozer.spec上还包括扩展名ttfs

#buildozer.spec
source.include_exts = py,png,jpg,kv,atlas, ttfs, json

希望有帮助。