TypefaceHelper
1.0.0
助手对象将字体注入Android的各种文本视图中。
我们可以对任何文本视图(例如TextView,Button,RadioButton,EditText等)使用各种自定义字体资产,但是无法将字体视为样式主题来应用应用程序中的整体屏幕。
该库有助于以简单的方式进行:)
而且还有一个严重的错误是,从资产资源创建字体将导致内存泄漏(请参阅此链接以获取更多详细信息),此库也将注意此问题。
首先,将您的字体放入asset目录中。
在您的应用程序类中,请注意辅助对象生命周期。
public class MyApp extends Application {
@ Override
public void onCreate () {
super . onCreate ();
TypefaceHelper . initialize ( this );
}
@ Override
public void onTerminate () {
TypefaceHelper . destroy ();
super . onTerminate ();
}
}在您的活动中,如果您想将字体设置为文本视图,
public class MyActivity extends Activity {
@ Override
public void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState );
setContentView ( R . layout . activity_main );
TextView hello = ( TextView ) findViewById ( R . id . hello_world );
TypefaceHelper . getInstance (). setTypeface ( hello , "font/font_file.ttf" );
}
}您还可以为所有属于特定视图组的文本视图设置字体。
public class MyActivity extends Activity {
@ Override
public void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState );
setContentView ( R . layout . activity_main );
LinearLayout container = ( LinearLayout ) findViewById ( R . id . text_container );
TypefaceHelper . getInstance (). setTypeface ( container , "font/font_file.ttf" );
}
}如果要在活动布局下应用所有文本视图的字体,
public class MyActivity extends Activity {
@ Override
public void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState );
setContentView ( TypefaceHelper . getInstance (). setTypeface ( this , R . layout . activity_main , "font/font_file.ttf" ));
}
}很好,简单!
您可以像这样将字体应用到整个窗口。
public class MyActivity extends Activity {
@ Override
public void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState );
setContentView ( R . layout . layout_activity_main );
TypefaceHelper . getInstance (). setTypeface ( this , "font/font_file.ttf" );
}
}...您也可以将字体名称作为字符串资源ID传递:
public class MyActivity extends Activity {
@ Override
public void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState );
setContentView ( R . layout . layout_activity_main );
TypefaceHelper . getInstance (). setTypeface ( this , R . string . font_primary );
}
}Gradle:
compile 'com.drivemode:TypefaceHelper:1.2.0@aar'
Copyright (C) 2014 Drivemode, Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
示例应用程序在OFL下使用以下字体。