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下使用以下字體。