我试图在Android Studio中使用自定义字体,就像我们在Eclipse中所做的那样。但不幸的是,不知道在哪里放“资产”文件夹!
当前回答
你好,这里我们有一个更好的方法应用在android上的EditTexts和TextViews字体,并将其应用于整个项目。
首先,你需要使字体文件夹。以下是步骤。
1:进入(项目文件夹)然后应用>src>main
2:在主文件夹中创建名为assets/fonts的文件夹。
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"
/>
其他回答
芬兰湾的科特林回答
如果你需要在代码方面使用字体,你可以使用这个功能,它也有版本代码控制。
fun getFontJarvisWhite(): Typeface {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) resources.getFont(R.font.jarvis_white)
else context?.let { ResourcesCompat.getFont(it, R.font.jarvis_white) }!!
}
如果你像我一样对Android非常陌生,这可能有点棘手。请务必致电:
TextView myTextView = (TextView) findViewById(R.id.textView);
Typeface typeface=Typeface.createFromAsset(getAssets(), "fonts/your font.ttf");
myTextView.setTypeface(typeface);
方法,例如onCreate。
首先创建资产文件夹,然后在其中创建字体文件夹。
然后你可以设置字体从资产或目录如下:
public class FontSampler extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.custom);
Typeface face = Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf");
tv.setTypeface(face);
File font = new File(Environment.getExternalStorageDirectory(), "MgOpenCosmeticaBold.ttf");
if (font.exists()) {
tv = (TextView) findViewById(R.id.file);
face = Typeface.createFromFile(font);
tv.setTypeface(face);
} else {
findViewById(R.id.filerow).setVisibility(View.GONE);
}
}
}
选择“File>New>Folder>Assets Folder” 单击finish 右键单击资产并创建一个名为fonts的文件夹 把你的字体文件在资产>字体 使用下面的代码改变你的textView的字体 TextView TextView = (TextView) findViewById(R.id.textView); 字体字体=字体字体。createfromasset (getAssets(), "fonts/yourfont.ttf"); textView.setTypeface(字体);
把字体放在资产文件夹 然后应用fontfamily: " your fonts
推荐文章
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- 如何设置整个应用程序在纵向模式?
- Android中文本的阴影效果?
- 以编程方式设置TextView的布局权重
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 如何从通知点击发送参数到一个活动?
- 导航目标xxx对于这个NavController是未知的
- 使用ConstraintLayout均匀间距的视图
- 文件google-services错误。模块根文件夹中缺少Json。谷歌服务插件没有它就不能正常工作。