我正在开发一个应用程序,用户在网络中选择一个字体(他有大约3或4个选择)。应用程序必须在整个应用程序中具有选定的字体,因此我需要以编程方式更改它(不涉及XML )。我收到了用户通过web服务选择的字体。我已经搜索过网络,但是我所读到的所有解决方案都需要使用XML。
0-应用程序在资产文件夹中有一些(3-4) .ttf文件。
用户在网络中选择一个字体。
Android应用程序启动,并获得一个字符串常量(或int),表示用户选择的字体。
3- Android应用程序根据用户选择在整个应用程序中更改字体。
有办法做到这一点吗?
发布于 2020-03-05 10:30:54
要以编程方式更改整个应用程序中的字体,可以尝试书法库
https://github.com/InflationX/Calligraphy
初始化字体将在Application类中,因此我相信您可以实现您所要求的
ViewPump.init(ViewPump.builder()
.addInterceptor(new CalligraphyInterceptor(
new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/Roboto-RobotoRegular.ttf")
.setFontAttrId(R.attr.fontPath)
.build()))
.build());你可以做这样的事
String fontPath;
if(userSelection == 0) { //userSelection value from the server
fontPath = "fonts/FirstFont.ttf"
} else if (userSelection == 1) {
fontPath = "fonts/SecondFont.ttf"
} else {
fontPath = "fonts/ThirdFont.ttf"
}
ViewPump.init(ViewPump.builder()
.addInterceptor(new CalligraphyInterceptor(
new CalligraphyConfig.Builder()
.setDefaultFontPath(fontPath)
.setFontAttrId(R.attr.fontPath)
.build()))
.build());我以为您可以从服务器检索用户选择
https://stackoverflow.com/questions/60542877
复制相似问题