我需要在我的Android项目中应用自定义字体。将不胜感激。
这是我在此链接上发布的答案。如何在Android Studio中使用自定义字体在这里,我们有一种更好的方法来一次在Android上应用字体,并将其应用于整个项目。
首先,您需要制作字体文件夹。这是步骤。
1:转到(项目文件夹),然后app> src> main
2:在主文件夹中创建名为"资产/字体"的文件夹。
3:将字体放入字体文件夹中。在这里,我有'mavenpro-regular.ttf'
这是在EditText上应用自定义字体的步骤,并且使用此方法可以在每个输入上应用字体。
1:创建一个类myEdittext(您的首选名称...)
2:扩展了EditText
3:应用您的字体
这是代码示例;
public class MyEditText extends EditText {
public MyEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyEditText(Context context) {
super(context);
init();
}
private void init() {
if (!isInEditMode()) {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/MavenPro-Regular.ttf");
setTypeface(tf);
}
}
}
,在这里是如何使用它的代码。
MyEditText editText = (MyEditText) findViewById(R.id.editText);
editText.setText("Hello");
或XML文件中的
<MyEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#fff"
android:textSize="16dp"
android:id="@+id/editText"
/>