【问题标题】:can the font type of an Edittext,Radio Button and CheckBox be changed in Android可以在 Android 中更改 Edittext、Radio Button 和 CheckBox 的字体类型吗
【发布时间】:2020-12-12 14:51:24
【问题描述】:

我是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); 

上面的代码是我用来更改文本视图的字体的。但我还需要更改单选按钮、编辑文本和复选框(我在我的应用程序中也使用过)文本的字体类型。请在这里帮助我。在此先感谢。

【问题讨论】:

  • 我猜,您也必须对其他视图使用相同的代码。您是否尝试过使用代码编辑文本等??
  • 是的,您也必须对 edittext 使用相同的代码。

标签: android checkbox radio-button android-edittext


【解决方案1】:

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

编辑文本

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) layout.findViewById(R.id.radiobutton);
radioButton.setText(msg);
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/myfont.ttf");
radioButton.setTypeface(font);

复选框

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

多个视图

如果您需要为应用程序中的多个视图执行此操作,那么创建EditText、RadioButton 或CheckBox 的子类可能会更容易。这个子类设置字体。以下是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);
    }
}

可以在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"/>

【讨论】:

    【解决方案2】:

    是的,您必须遵循您在此处提到的相同代码。这也适用于其他控件,例如 Edittext、CheckBox 等。

    【讨论】:

    • 没有资产文件夹怎么办? (带有android中新的字体文件夹)
    【解决方案3】:

    您也可以在 .xml 文件中这样做

    你可以使用属性来这样做

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

    您可以将它们与按钮、复选框等一起使用。

    你也可以通过代码来做

    【讨论】:

      【解决方案4】:

      是的,同样的方法也适用于按钮和单选按钮。
      您也可以通过以下简单步骤完成此操作

      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.
      

      【讨论】:

        【解决方案5】:

        确保您的支持库 >=26.0 或在您的项目中使用 AndroidX。然后将此行添加到您的 XML 视图中

              app:fontFamily="@font/myFontFamily"
        

        例如:

          <RadioButton
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/language_english"
              android:text="@string/english"
              app:fontFamily="@font/myFontFamily"
              />
        

        如果要使用自定义字体,请阅读官方文档: https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml

        【讨论】:

          猜你喜欢
          • 2012-06-01
          • 1970-01-01
          • 1970-01-01
          • 2014-07-29
          • 2014-09-20
          • 2017-08-25
          • 2011-05-07
          • 1970-01-01
          • 2011-04-04
          相关资源
          最近更新 更多