自定义字体和 XML 布局 (Android)
我正在尝试在 Android 中使用 XML 文件定义 GUI 布局。据我所知,没有办法指定您的小部件应该在 XML 文件中使用自定义字体(例如您放置在 asset/font/ 中的字体),并且您只能使用系统安装的字体。
我知道,在 Java 代码中,我可以使用唯一的 ID 手动更改每个小部件的字体。或者,我可以迭代 Java 中的所有小部件来进行此更改,但这可能会非常慢。
我还有什么其他选择?有没有更好的方法来制作具有自定义外观的小部件?我并不特别希望必须手动更改我添加的每个新小部件的字体。
I'm trying to define a GUI layout using XML files in Android. As far as I can find out, there is no way to specify that your widgets should use a custom font (e.g. one you've placed in assets/font/) in XML files and you can only use the system installed fonts.
I know that, in the Java code, I could change the font of each widget manually using unique IDs. Alternatively, I could iterate over all the widgets in Java to make this change, but this would probably be very slow.
What other options do I have? Is there any better ways to making widgets that have a custom look? I don't particularly want to have to manually change the font for every new widget I add.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
您可以扩展 TextView 来设置自定义字体,正如我此处。
TextViewPlus.java:
attrs.xml:(在 res/values 中)
main.xml:
您可以将“saxmono.ttf”放入 < em>assets 文件夹。
更新 8/1/13
此方法存在严重的内存问题。请参阅下面的 chedabob 的评论。
You can extend TextView to set custom fonts as I learned here.
TextViewPlus.java:
attrs.xml: (in res/values)
main.xml:
You would put "saxmono.ttf" in the assets folder.
UPDATE 8/1/13
There are serious memory concerns with this method. See chedabob's comment below.
我迟到了 3 年:( 不过,这对于可能偶然发现这篇文章的人来说可能很有用。
我编写了一个缓存字体的库,还允许您直接从 XML 指定自定义字体。您可以找到库此处
是您使用 XML 布局时的样子。
I'm 3 years late for the party :( However this could be useful for someone who might stumble upon this post.
I've written a library that caches Typefaces and also allow you to specify custom typefaces right from XML. You can find the library here.
Here is how your XML layout would look like, when you use it.
这可能有点晚了,但您需要创建一个返回自定义字体的单例类以避免内存泄漏。
TypeFace 类:
自定义 TextView:
通过 xml:
以编程方式:
This might be a little late, but you need to create a singleton class that returns the custom typeface to avoid memory leaks.
TypeFace class:
Custom TextView:
By xml:
Programmatically:
老问题,但我确实希望在开始自己寻找一个好的解决方案之前先在这里阅读这个答案。 Calligraphy 扩展了
android:fontFamily
属性,以在资源中添加对自定义字体的支持文件夹,如下所示:要激活它,您唯一要做的就是将其附加到您正在使用的 Activity 的上下文:
您还可以指定自己的自定义属性来替换
android:fontFamily
它也适用于主题,包括 AppTheme。
Old question, but I sure wish I read this answer here before I started my own search for a good solution. Calligraphy extends the
android:fontFamily
attribute to add support for custom fonts in your asset folder, like so:The only thing you have to do to activate it is attaching it to the Context of the Activity you're using:
You can also specify your own custom attribute to replace
android:fontFamily
It also works in themes, including the AppTheme.
使用DataBinding:
在 XML 中:
字体文件必须位于
assets/fonts/
中Using DataBinding :
In XML:
font file must be in
assets/fonts/
如果您只想添加一种字体,并且希望编写更少的代码,则可以为您的特定字体创建专用的 TextView。请参阅下面的代码。
在 main.xml 中,您现在可以像这样添加 textView:
If you only have one typeface you would like to add, and want less code to write, you can create a dedicated TextView for your specific font. See code below.
In main.xml, you can now add your textView like this:
从 Android O 预览版开始,最好的方法是这样
1.)右键单击res文件夹并转到新建> Android资源目录。新
出现资源目录窗口。
2.)在资源类型列表中,选择字体,然后单击确定。
3.)在字体文件夹中添加字体文件。下面的文件夹结构会生成 R.font.dancing_script、R.font.la_la 和 R.font.ba_ba。
4.)双击字体文件可在编辑器中预览文件的字体。
接下来我们必须创建一个字体系列
1.) 右键单击字体文件夹并转到新建> >字体资源文件。将出现“新建资源文件”窗口。
2.) 输入文件名,然后单击“确定”。新字体资源 XML 将在编辑器中打开。
3.) 将每个字体文件、样式和粗细属性包含在字体标签元素中。以下 XML 说明了如何在字体资源 XML 中添加与字体相关的属性:
将字体添加到 TextView:
如文档所示
所有步骤都是正确的。
The best way to do it From Android O preview release is this way
1.)Right-click the res folder and go to New > Android resource directory. The New
Resource Directory window appears.
2.)In the Resource type list, select font, and then click OK.
3.)Add your font files in the font folder.The folder structure below generates R.font.dancing_script, R.font.la_la, and R.font.ba_ba.
4.)Double-click a font file to preview the file's fonts in the editor.
Next we must create a font family
1.)Right-click the font folder and go to New > Font resource file. The New Resource File window appears.
2.)Enter the file name, and then click OK. The new font resource XML opens in the editor.
3.)Enclose each font file, style, and weight attribute in the font tag element. The following XML illustrates adding font-related attributes in the font resource XML:
Adding fonts to a TextView:
As from the documentation
all the steps are correct.
扩展
TextView
并给它一个自定义属性,或者仅使用 android:tag 属性传入您要使用的字体的字符串。您需要选择一个约定并遵守它,例如我将把所有字体放在 res/assets/fonts/ 文件夹中,以便您的 TextView 类知道在哪里找到它们。然后在构造函数中,您只需在超级调用后手动设置字体即可。Extend
TextView
and give it a custom attribute or just use the android:tag attribute to pass in a String of what font you want to use. You will need to pick a convention and stick to it such as I will put all of my fonts in the res/assets/fonts/ folder so your TextView class knows where to find them. Then in your constructor you just set the font manually after the super call.使用自定义字体的唯一方法是通过源代码。
请记住,Android 运行在资源非常有限的设备上,并且字体可能需要大量 RAM。内置的 Droid 字体是专门制作的,如果您注意到的话,会缺少许多字符和装饰。
The only way to use custom fonts is through the source code.
Just remember that Android runs on devices with very limited resources and fonts might require a good amount of RAM. The built-in Droid fonts are specially made and, if you note, have many characters and decorations missing.
我可能会对这个问题有一个简单的答案,而无需扩展 TextView 并实现长代码。
代码:
像往常一样将自定义字体文件放入资产文件夹中并尝试此操作。这对我有用。
我只是不明白为什么彼得为这个简单的事情给出了这么大的代码,或者他在旧版本中给出了答案。
I might have a simple answer for the question without extending the TextView and implementing a long code.
Code :
Place the custom font file in assets folder as usual and try this. It works for me.
I just dont understand why peter has given such a huge code for this simple thing or he has given his answer in old version.
也可以在 xml 中定义,而无需创建自定义类
style.xml
Activity_main.xml
快速说明,因为我总是忘记将字体放在哪里,所以字体必须位于
assets
中该文件夹与res
和src
位于同一级别,在我的例子中是assets/fonts/ionicons.ttf
< strong>更新了添加了根布局,因为此方法需要
xmlns:app="http://schemas.android.com/apk/res-auto"
才能工作更新2< /strong> 忘记了我之前安装的一个名为 Calligraphy 的库
Also can be defined in the xml without creating custom classes
style.xml
activity_main.xml
A quick note, because I just always forgot where to put the fonts, its that the font must be inside
assets
and this folder resides in the same level thatres
andsrc
, in my case itsassets/fonts/ionicons.ttf
Updated Added root layout because this method needs
xmlns:app="http://schemas.android.com/apk/res-auto"
to workUpdate 2 Forgot about a library that I've installed before called Calligraphy
Peter 的答案是最好的,但可以通过使用 Android 中的 styles.xml 为应用程序中的所有文本视图自定义字体来改进它。
我的代码是 这里
Peter's answer is the best, but it can be improved by using the styles.xml from Android to customize your fonts for all textviews in your app.
My code is here
自定义字体有两种方法:
!!!我的自定义字体位于 asset/fonts/iran_sans.ttf
方式 1 :
反射 Typeface.class ||| 最好的方法
在继承Application的类中调用FontsOverride.setDefaultFont(),此代码将导致所有软件字体发生变化,甚至Toasts字体
AppController.java
FontsOverride.java
方式2:使用setTypeface
特殊视图只需调用setTypeface()来改变字体。
CTextView.java
FontUtils.java
There are two ways to customize fonts :
!!! my custom font in assets/fonts/iran_sans.ttf
Way 1 :
Refrection Typeface.class ||| best way
call FontsOverride.setDefaultFont() in class extends Application, This code will cause all software fonts to be changed, even Toasts fonts
AppController.java
FontsOverride.java
Way 2: use setTypeface
for special view just call setTypeface() to change font.
CTextView.java
FontUtils.java
这是一个教程,向您展示如何设置像 @peter 描述的自定义字体: http://responsiveandroid.com/2012/03/15/custom-fonts-in-android-widgets.html
它还考虑了潜在的内存泄漏 http://code.google.com/p/android/issues/detail?id=9904 。本教程中还提供了在按钮上设置自定义字体的示例。
Here's a tutorial that shows you how to setup a custom font like @peter described: http://responsiveandroid.com/2012/03/15/custom-fonts-in-android-widgets.html
it also has consideration for potential memory leaks ala http://code.google.com/p/android/issues/detail?id=9904 . Also in the tutorial is an example for setting a custom font on a button.
您可以轻松创建自定义 textview 类:-
因此,您首先需要做的是,创建使用
AppCompatTextView
扩展的Custom textview
类。现在,每次调用自定义文本视图时,我们都会从属性中获取 int 值 int fontValue = attrs.getAttributeIntValue("http://schemas.android.com/apk/res-auto","Lato-Regular.ttf “,-1)。
或者
我们也可以从我们在 xml 中设置的视图获取 getTypeface() (
android:textStyle="bold|normal|italic"
)。所以,做你想做的事吧。现在,我们制作
FontUtils
来将任何 .ttf 字体设置到我们的视图中。You can make easily custom textview class :-
So what you need to do first, make
Custom textview
class which extended withAppCompatTextView
.Now , every time Custom text view call and we will get int value from attribute
int fontValue = attrs.getAttributeIntValue("http://schemas.android.com/apk/res-auto","Lato-Regular.ttf",-1)
.Or
We can also get getTypeface() from view which we set in our xml (
android:textStyle="bold|normal|italic"
). So do what ever you want to do.Now, we make
FontUtils
for set any .ttf font into our view.了解从 Android 8.0(API 级别 26)开始,您可以 在 XML 中使用自定义字体。
您可以通过以下方式将自定义字体应用于整个应用程序。
将字体放入文件夹
res/font
。在
res/values/styles.xml
中在应用程序主题中使用它。<代码>
It may be useful to know that starting from Android 8.0 (API level 26) you can use a custom font in XML.
You can apply a custom font to the entire application in the following way.
Put the font in the folder
res/font
.In
res/values/styles.xml
use it in the application theme.<style name="AppTheme" parent="{whatever you like}">
<item name="android:fontFamily">@font/myfont</item>
</style>
Fontinator 是一个 Android 库,可以轻松使用自定义字体。
https://github.com/svendvd/Fontinator
Fontinator is an Android-Library make it easy, to use custom Fonts.
https://github.com/svendvd/Fontinator
您无法扩展 TextView 来创建小部件或在小部件布局中使用小部件:
http://developer.android.com/guide/topics/appwidgets/index.html
You can't extend TextView to create a widget or use one in a widgets layout:
http://developer.android.com/guide/topics/appwidgets/index.html