我试图在Android Studio中使用自定义字体,就像我们在Eclipse中所做的那样。但不幸的是,我想不出应该把'assets‘文件夹放在哪里!
发布于 2016-08-14 13:20:58
您好,在这里,我们有一个更好的方式来应用字体的EditTexts和安卓系统的TextViews一次,并在整个项目中应用它。
首先你需要制作fonts文件夹。以下是步骤。
1:转到(项目文件夹),然后app>src>main
2:在主文件夹中创建名为'assets/fonts‘的文件夹。
3:将您的字体放入fonts文件夹。这里有'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"
/>
发布于 2017-05-19 22:22:21
使用Support Library 26.0 (和Android O)字体可以轻松地从资源加载,使用:
Typeface typeface = ResourcesCompat.getFont(Context context, int fontResourceId)
欲了解更多信息,请访问here.。
发布于 2015-06-26 02:10:44
您可以使用简单易用的EasyFonts第三方库来为您的TextView
设置各种自定义字体。通过使用该库,您不必担心下载字体并将其添加到assets/fonts文件夹中。还可以创建Typeface对象。你也可以不用创建资产文件夹了。
简单地说:
TextView myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setTypeface(EasyFonts.robotoThin(this));
这个库提供了许多类型的字体。
https://stackoverflow.com/questions/27588965
复制相似问题