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

发布于 2025-01-08 11:50:18 字数 383 浏览 0 评论 0原文

我是 android 的初学者。我可以更改 Android 中 Textview 的字体类型。但是我必须使用 asset 文件夹中的 .ttf 文件来实现这种字体更改。

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

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

I am a beginner in android.I can able to change the font type of a Textview in Android.But I have to use .ttf file in asset folder,to bring this kind of font change.

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

The above code is what I used to change the font of a text View.but I need to change the font type of the text of Radio Button,Edittext and Check box(which Im also used in my application) as well.Plz help me out here.Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我们只是彼此的过ke 2025-01-15 11:50:18

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

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

多个视图

如果您需要对跨多​​个视图执行此操作应用程序,那么创建 EditTextRadioButtonCheckBox 的子类可能会更容易。该子类设置字体。下面是 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"/>

The selected answer was missing the code, so here it is:

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

Multiple Views

If you need to do this for multiple views across your application, then it may be easier to make a subclass of your EditText, RadioButton, or CheckBox. This subclass sets the font. Below is an example for 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);
    }
}

It can be used in xml as follows:

<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"/>
赠意 2025-01-15 11:50:18

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

Yes you have to follow the same code wht u have mentioned here.This will work for other controls too like Edittext,CheckBox etc.

醉梦枕江山 2025-01-15 11:50:18

您也可以在 .xml 文件中执行此操作,也

可以使用 proertiees 来执行此操作,因此

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

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

您也可以通过代码执行此操作

you can do it so in .xml file as well

you can use proertiees to do so

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

you can use these with buttons , checboxes etc.

u can do the by code also

梦亿 2025-01-15 11:50:18

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

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.

Yes the same approach will work for buttons and radio buttons too.
Also you can do this in simple steps as follows

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.
子栖 2025-01-15 11:50:18

确保您的支持库>=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/指南/主题/ui/look-and-feel/fonts-in-xml

Make sure your support library is >=26.0 or using AndroidX in your project. Then add this line to your view in XML

      app:fontFamily="@font/myFontFamily"

e.g:

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

If you want to use your custom font, read the official document:
https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文