1. Trang chủ
  2. » Thể loại khác

custom font trong android

5 133 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Nội dung

http://vietjack.com/android/index.jsp                                                                                                              Copyright  ©  vietjack.com     Custom Font Android Trong Android, bạn định nghĩa cho riêng Custom Font cho chuỗi ứng dụng Bạn cần tải Font bạn muốn từ Internet, sau đặt folder assets/fonts Sau đặt Font assets folder fonts folder, bạn truy cập Java code thông qua lớp Typeface Đầu tiên, lấy tham chiếu Text View code Cú pháp là: − TextView tx = (TextView)findViewById(R.id.textview1); Điều bạn cần làm gọi phương thức static lớp Typeface làcreateFromAsset() để lấy Custom Font từ assets folder Cú pháp sau: − Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/font name.ttf"); Cuối cùng, bạn thiết lâp đối tượng Custom Font tới thuộc tính TextView Typeface Bạn cần gọi phương thức setTypeface() để thực điều Cú pháp sau: − tx.setTypeface(custom_font); Ngồi phương thức trên, lớp Typeface định nghĩa số phương thức khác để bạn xử lý Font hiệu Stt Phương thức & Miêu tả create(String familyName, int style) Tạo đối tượng Typeface với familyname cho thông tin style create(Typeface family, int style) Tạo đối tượng Typeface mà kết nối với Typeface tồn Style xác định createFromFile(String path) http://vietjack.com/                                                                                                                              Trang  chia  sẻ  các  bài  học  online  miễn  phí  Page  1   http://vietjack.com/android/index.jsp                                                                                                              Copyright  ©  vietjack.com     Tạo đối tượng từ fonts file cho defaultFromStyle(int style) Trả đối tượng Typeface mặc định, dựa style cho getStyle() Trả thuộc tính style nội đối tượng Typeface Ví dụ Ví dụ sau minh họa sử dụng Typeface để xử lý Custom Font Nó tạo ứng dụng đơn giản mà hiển thị Custom Font mà bạn xác định fonts file Để thực nghiệm ví dụ, bạn cần chạy thiết bị thực Emulator Sau nội dung Main Activity file sửa đổi: MainActivity.java package com.example.sairamkrishna.myapplication; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Typeface; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends ActionBarActivity { TextView tv1,tv2; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv1=(TextView)findViewById(R.id.textView3); tv2=(TextView)findViewById(R.id.textView4); Typeface face= Typeface.createFromAsset(getAssets(), "font/font.ttf"); tv1.setTypeface(face); Typeface face1= Typeface.createFromAsset(getAssets(), "font/font1.ttf"); tv2.setTypeface(face1); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } Bạn sửa đổi nội dung activity_main.xml http://vietjack.com/                                                                                                                              Trang  chia  sẻ  các  bài  học  online  miễn  phí  Page  1   http://vietjack.com/android/index.jsp                                                                                                              Copyright  ©  vietjack.com     Và nội dung res/values/string.xml My Application name="hello_world">Hello world! Settings

Ngày đăng: 02/12/2017, 07:11

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN