我需要使用特定的字体为我的整个应用程序。我有。ttf文件相同。 是否有可能将此设置为默认字体,在应用程序启动,然后在应用程序的其他地方使用它?当设置,我如何使用它在我的布局xml ?
当前回答
Tom的解决方案很好,但只适用于TextView和EditText。
如果你想覆盖大部分的视图(RadioGroup, TextView,复选框…),我创建了一个方法:
protected void changeChildrenFont(ViewGroup v, Typeface font){
for(int i = 0; i < v.getChildCount(); i++){
// For the ViewGroup, we'll have to use recursivity
if(v.getChildAt(i) instanceof ViewGroup){
changeChildrenFont((ViewGroup) v.getChildAt(i), font);
}
else{
try {
Object[] nullArgs = null;
//Test wether setTypeface and getTypeface methods exists
Method methodTypeFace = v.getChildAt(i).getClass().getMethod("setTypeface", new Class[] {Typeface.class, Integer.TYPE});
//With getTypefaca we'll get back the style (Bold, Italic...) set in XML
Method methodGetTypeFace = v.getChildAt(i).getClass().getMethod("getTypeface", new Class[] {});
Typeface typeFace = ((Typeface)methodGetTypeFace.invoke(v.getChildAt(i), nullArgs));
//Invoke the method and apply the new font with the defined style to the view if the method exists (textview,...)
methodTypeFace.invoke(v.getChildAt(i), new Object[] {font, typeFace == null ? 0 : typeFace.getStyle()});
}
//Will catch the view with no such methods (listview...)
catch (Exception e) {
e.printStackTrace();
}
}
}
}
该方法将返回视图集的XML样式(粗体,斜体…),并在它们存在时应用它们。
对于ListView,我总是创建一个适配器,并在getView中设置字体。
其他回答
android中有一个很棒的自定义字体库:Calligraphy
这里有一个如何使用它的示例。
在Gradle中,你需要将这一行放入应用程序的构建中。gradle文件:
dependencies {
compile 'uk.co.chrisjenx:calligraphy:2.2.0'
}
然后创建一个扩展Application的类,并编写如下代码:
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("your font path")
.setFontAttrId(R.attr.fontPath)
.build()
);
}
}
在activity类中将这个方法放在onCreate之前:
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
最后一件事你的清单文件应该是这样的:
<application
.
.
.
android:name=".App">
它将改变整个活动,以您的字体!既简单又干净!
为萨玛林工作。Android:
类:
public class FontsOverride
{
public static void SetDefaultFont(Context context, string staticTypefaceFieldName, string fontAssetName)
{
Typeface regular = Typeface.CreateFromAsset(context.Assets, fontAssetName);
ReplaceFont(staticTypefaceFieldName, regular);
}
protected static void ReplaceFont(string staticTypefaceFieldName, Typeface newTypeface)
{
try
{
Field staticField = ((Java.Lang.Object)(newTypeface)).Class.GetDeclaredField(staticTypefaceFieldName);
staticField.Accessible = true;
staticField.Set(null, newTypeface);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
应用程序实现:
namespace SomeAndroidApplication
{
[Application]
public class App : Application
{
public App()
{
}
public App(IntPtr handle, JniHandleOwnership transfer)
: base(handle, transfer)
{
}
public override void OnCreate()
{
base.OnCreate();
FontsOverride.SetDefaultFont(this, "MONOSPACE", "fonts/Roboto-Light.ttf");
}
}
}
风格:
<style name="Theme.Storehouse" parent="Theme.Sherlock">
<item name="android:typeface">monospace</item>
</style>
在API 26中使用build。Gradle 3.0.0及更高版本可以在res中创建字体目录 把这句话用在你的风格里
<item name="android:fontFamily">@font/your_font</item>
对于变更构建。Gradle在你的构建中使用这个。gradle dependecies
classpath 'com.android.tools.build:gradle:3.0.0'
最后,谷歌意识到这个问题的严重性(将自定义字体应用到UI组件),他们为它设计了一个干净的解决方案。
首先,你需要更新到支持库26+(你可能还需要更新你的gradle{4.0+}, android studio),然后你可以创建一个新的资源文件夹称为字体。在这个文件夹中,您可以放置您的字体资源(.tff,…) 然后你需要覆盖默认的应用程序他们,并强制您的自定义字体:)
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:fontFamily">@font/my_custom_font</item>
</style>
注意:如果你想要支持API超过16的设备,你必须使用app命名空间而不是android!
是的,可以将字体设置为整个应用程序。
实现这一点的最简单方法是将所需的字体打包到应用程序中。
要做到这一点,只需在项目根目录中创建一个assets/文件夹,并将您的字体(放在 TrueType,或TTF表单)。
例如,您可以创建资产/字体/并将您的TTF文件放在那里。
public class FontSampler extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
TextView tv=(TextView)findViewById(R.id.custom);
Typeface face=Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf");
tv.setTypeface(face);
}
}
推荐文章
- 如何找到Java堆大小和内存使用(Linux)?
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 使用Enum实现单例(Java)
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- RabbitMQ与通道和连接之间的关系
- buildSessionFactory()配置方法在Hibernate中已弃用?
- Spring MVC -如何获得所有的请求参数在一个地图在Spring控制器?
- 如何设置整个应用程序在纵向模式?
- 如何在Java中按两个字段排序?
- Android中文本的阴影效果?