วัตถุผู้ช่วยสำหรับการฉีดแบบอักษรลงในมุมมองข้อความต่าง ๆ ของ Android
เราสามารถใช้สินทรัพย์แบบอักษรที่กำหนดเองต่าง ๆ สำหรับมุมมองข้อความใด ๆ (เช่น TextView, ปุ่ม, 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" );
}
}และ ... คุณสามารถส่งชื่อตัวอักษรเป็นรหัสทรัพยากรสตริง:
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