我使用Roboto字体光在我的应用程序。要设置我已经添加了 Android的字体:fontFamily中=无衬线光
的每一个观点。有没有什么办法来声明Roboto字体作为默认字体家族整个应用程序?我已经试过这样,但它似乎并没有工作。
<样式名称=AppBaseTheme父=机器人:Theme.Light>< /风格>
<样式名称=AppTheme父=AppBaseTheme>
<项目名称=机器人:fontFamily中>无衬线光< /项目>
< /风格>
解决方案
答案是肯定的。
全球Roboto光的TextView
和按钮
类。
<样式名称=AppTheme父=AppBaseTheme>
<项目名称=机器人:textViewStyle> @风格/ RobotoTextViewStyle< /项目>
<项目名称=机器人:按钮样式> @风格/ RobotoButtonStyle< /项目>
< /风格>
<样式名称=RobotoTextViewStyle父=机器人:Widget.TextView>
<项目名称=机器人:fontFamily中>无衬线光< /项目>
< /风格>
<样式名称=RobotoButtonStyle父=机器人:Widget.Holo.Button>
<项目名称=机器人:fontFamily中>无衬线光< /项目>
< /风格>
只需选择风格,你想从列表themes.xml那么在原有基础上创建自定义样式。在结束应用样式的应用程序的主题。
<应用
机器人:主题=@风格/ AppTheme>
< /用途>
这将仅适用于像Roboto内置字体,但是这是问题。 对于自定义字体(从资产加载为例)这个方法是行不通的。
修改15年8月13日
如果您使用的是AppCompat主题记得删除机器人:
preFIX。例如:
<样式名称=AppTheme父=Theme.AppCompat.Light.DarkActionBar>
<项目名称=机器人:textViewStyle> @风格/ RobotoTextViewStyle< /项目>
<项目名称=按钮样式> @风格/ RobotoButtonStyle< /项目>
< /风格>
注意按钮样式
不包含机器人:
preFIX,但 textViewStyle
必须包含它。
I'm using the Roboto light font in my app. To set the font I've to add the android:fontFamily="sans-serif-light"
to every view. Is there any way to declare the Roboto font as default font family to entire app? I've tried like this but it didn't seem to work.
<style name="AppBaseTheme" parent="android:Theme.Light"></style>
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:fontFamily">sans-serif-light</item>
</style>
解决方案
The answer is yes.
Global Roboto light for TextView
and Button
classes.
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:textViewStyle">@style/RobotoTextViewStyle</item>
<item name="android:buttonStyle">@style/RobotoButtonStyle</item>
</style>
<style name="RobotoTextViewStyle" parent="android:Widget.TextView">
<item name="android:fontFamily">sans-serif-light</item>
</style>
<style name="RobotoButtonStyle" parent="android:Widget.Holo.Button">
<item name="android:fontFamily">sans-serif-light</item>
</style>
Just select the style you want from list themes.xml then create your custom style based on the original. At the end apply the style as the theme of the application.
<application
android:theme="@style/AppTheme" >
</application>
It will works only with built-in fonts like Roboto, but that was the question. For custom fonts (loaded from assets for example) this method will not work.
EDIT 08/13/15
If you're using AppCompat themes remember to remove android:
prefix. For example:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:textViewStyle">@style/RobotoTextViewStyle</item>
<item name="buttonStyle">@style/RobotoButtonStyle</item>
</style>
Note the buttonStyle
doesn't contain android:
prefix, but textViewStyle
must contain it.