可以在Android中更改Edittext,Radio Button和CheckBox的字体类型

use*_*764 6 checkbox android radio-button android-edittext

我是android的初学者.我可以在Android中更改Textview的字体类型.但是我必须在资产文件夹中使用.ttf文件,才能进行这种字体更改.

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

上面的代码是我用来改变文本View.but的字体.我需要更改单选按钮,Edittext和复选框(我也在我的应用程序中使用)的文本的字体类型.Plz帮助我在这里.谢谢你.

小智 6

是的,你必须遵循你在这里提到的相同的代码.这将适用于其他控件,如Edittext,CheckBox等.


Sur*_*gch 5

所选答案缺少代码,所以这里是:

的EditText

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

单选按钮

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

复选框

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

多视图

如果您需要为您的整个应用程序的多个视图做到这一点,那么它可能会更容易让你的一个子类EditText,RadioButtonCheckBox.此子类设置字体.以下是一个例子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);
    }
}
Run Code Online (Sandbox Code Playgroud)

它可以在xml中使用如下:

<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"/>
Run Code Online (Sandbox Code Playgroud)