2012-02-22 46 views
5

Tôi là người mới bắt đầu trong android.I có thể thay đổi kiểu phông chữ của Textview trong Android.But tôi phải sử dụng tệp .ttf trong nội dung để mang loại thay đổi phông chữ này.kiểu phông chữ của Edittext, Radio Button và CheckBox được thay đổi trong Android

TextView text = (TextView) layout.findViewById(R.id.text); 
text.setText(msg); 
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/handsean.ttf"); 
text.setTypeface(font); 

Đoạn mã trên là những gì tôi sử dụng để thay đổi font chữ của một văn bản View.but tôi cần phải thay đổi kiểu font chữ của văn bản của Radio Button, EditText và Kiểm tra hộp (mà Im cũng được sử dụng trong ứng dụng của tôi) là well.Plz giúp tôi ra khỏi đây. Cảm ơn trước.

+0

tôi đoán, bạn phải sử dụng cùng mã cho các quan điểm khác nữa. Bạn đã thử sử dụng mã để EditText vv. ?? – noob

+0

Có bạn phải sử dụng cùng một mã cho edittext quá. – AndoAiron

Trả lời

6

Có bạn phải tuân theo cùng một mã Wht u đã đề cập here.This sẽ làm việc cho các điều khiển khác quá như EditText, CheckBox, vv

+2

mã ở đâu? –

0

Có phương pháp tương tự sẽ làm việc cho các nút bấm và nút radio quá.
Ngoài ra bạn có thể làm điều này trong bước đơn giản như sau

Button moreBtn = (Button) events.findViewById(R.id.promotion_event_more_btn); 
FontUtils.setTypeface(this, moreBtn, Constants.C_FONT);</code><br>Where "Constants.C_FONT" is set to the path of the font file present in the assets folder. 
0

bạn có thể làm điều đó như vậy trong tập tin .xml cũng

bạn có thể sử dụng proertiees để làm như vậy

android:textSize="20dp" 
    android:textStyle="bold" 
    android:textColor="any color" 
    android:textAppearance="any appearencde" 

bạn có thể sử dụng các nút này có nút, checbox, v.v.

u cũng có thể thực hiện theo mã số

3

Câu trả lời được lựa chọn đã mất tích mã, vì vậy ở đây là:

EditText

EditText editText = (EditText) layout.findViewById(R.id.edittext); 
editText.setText(msg); 
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/myfont.ttf"); 
editText.setTypeface(font); 

RadioButton

RadioButton radioButton = (RadioButton) layout.findViewById(R.id.radiobutton); 
radioButton.setText(msg); 
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/myfont.ttf"); 
radioButton.setTypeface(font); 

CheckBox

CheckBox checkBox = (CheckBox) layout.findViewById(R.id.checkbox); 
checkBox.setText(msg); 
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/myfont.ttf"); 
checkBox.setTypeface(font); 

Nhiều Lần

Nếu bạn cần phải làm điều này cho nhiều quan điểm trên ứng dụng của bạn, sau đó nó có thể được dễ dàng hơn để tạo ra một lớp con của EditText, RadioButton của bạn, hoặc CheckBox. Phân lớp này đặt phông chữ. Dưới đây là ví dụ cho CheckBox.

public class MyCheckBox extends CheckBox { 

    // Constructors 
    public MyCheckBox(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     init(); 
    } 
    public MyCheckBox(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 
    public MyCheckBox(Context context) { 
     super(context); 
     init(); 
    } 

    // This class requires myfont.ttf to be in the assets/fonts folder 
    private void init() { 
     Typeface tf = Typeface.createFromAsset(getContext().getAssets(), 
       "fonts/myfont.ttf"); 
     setTypeface(tf); 
    } 
} 

Nó có thể được sử dụng trong xml như sau:

<com.example.projectname.MyCheckBox 
    android:id="@+id/checkbox" 
    android:text="@string/msg" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:checked="true"/> 
Các vấn đề liên quan