我试图在Android Studio中使用自定义字体,就像我们在Eclipse中所做的那样。但不幸的是,不知道在哪里放“资产”文件夹!
当前回答
2021年更新:
在res文件夹中创建一个名为font的文件夹并复制您的字体
所有字体名称只能为:小写字母a-z、0-9或下划线。
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/abc_font" />
编程使用:
textView.setTypeface(ResourcesCompat.getFont(context, R.font.abc_font))
对于Android Studio 4.2+,现在甚至有一个菜单选项:
其他回答
对于新读者
你可以使用这个库 Gloxey定制字体视图
gradle依赖
dependencies{
compile 'io.gloxey.cfv:custom-font-views:1.0.2'
}
如何使用?
创建文件夹资产->字体。复制你的字体到字体文件夹。
使用属性app: font_name = "font_name_string"在视图上应用字体。
例子
<!--Font Names in srings.xml-->
<string name="aadhunik">aadhunik.ttf</string>
<string name="kung_fool">kungfool.ttf</string>
<string name="skrova">skrova.otf</string>
<string name="painting_in_the_sun_light">painting_in_the_sun_light.ttf</string>
<!--Include views in layout.xml-->
<io.gloxey.cfv.CFTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Aadhunik"
android:textColor="#ff00"
android:textSize="40sp"
app:font_name="@string/aadhunik" />
<io.gloxey.cfv.CFButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Kung Fool"
android:textColor="#154748"
app:font_name="@string/kung_fool" />
<io.gloxey.cfv.CFEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Hello world"
android:textSize="30sp"
app:font_name="@string/skrova" />
<io.gloxey.cfv.CFCheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Painting In The Sun Light"
android:textSize="30sp"
app:font_name="@string/painting_in_the_sun_light" />
你好,这里我们有一个更好的方法应用在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"
/>
我想补充一下我对Android- o和Android Studio 2.4的回答
在res文件夹下创建名为font的文件夹。下载要添加到项目示例谷歌fonts中的各种字体 在xml用户字体家族中 例子: < TextView android: fontFamily = " @font / indie_flower” android: layout_width = " wrap_content " android: layout_height = " wrap_content " android:填充= " 10 dp” android:文本= " @string / sample_text " / >
3.如果您希望它以编程的方式使用下面的代码
Typeface typeface = getResources().getFont(R.font.indie_flower);
textView.setTypeface(typeface);
要了解更多信息,请链接到我的博客文章字体样式的Android与Android Studio 2.4
我认为我们可以使用谷歌字体而不是下载。ttf文件。这很容易实现。你只需要遵循这些步骤。 步骤1)打开你的项目的layout.xml和属性中的文本视图的选择字体家族(作为参考截图附在后面)
步骤2)in字体家族选择更多字体..选项,如果你的字体不在那里。然后你会看到一个新的窗口将打开,在那里你可以输入你需要的字体,并从列表中选择所需的字体,即常规,粗体,斜体等。如下图所示。
步骤3)然后你会看到一个字体文件夹将自动生成在/res文件夹有你选择的字体xml文件。
然后你可以直接在xml中使用这个字体家族作为
android:fontFamily="@font/josefin_sans_bold"
或者从专业语法上讲,你可以使用
Typeface typeface = ResourcesCompat.getFont(this, R.font.app_font);
fontText.setTypeface(typeface);
根据Android O的新特性,XML字体资源作为新特性可用。
添加字体作为资源,在Android Studio中执行以下步骤:
1)右键单击res文件夹,进入New > Android资源目录。出现“新建资源目录”窗口。
2)在“资源类型”列表中选择“字体”,单击“确定”。
注意:资源目录名必须为字体。
3)在字体文件夹中添加你的字体文件。
您可以在新的资源类型font的帮助下访问字体资源。例如,要访问字体资源,请使用@font/myfont或R.font.myfont。
如。字体字体= getResources().getFont(R.font.myfont); textView.setTypeface(字体);
推荐文章
- Android加载JS包失败
- Android Studio, logcat在应用程序关闭后清理
- 在android中从上下文获取活动
- 无法解析主机"<URL here>"没有与主机名关联的地址
- getActivity()在Fragment函数中返回null
- 按钮背景是透明的
- 在Mac OS X上哪里安装Android SDK ?
- 我如何获得图像缩放功能?
- 在Android应用程序中显示当前时间和日期
- BottomSheetDialogFragment的圆角
- 在应用程序启动时出现“无法获得BatchedBridge,请确保您的bundle被正确打包”的错误
- 我如何改变默认对话框按钮的文本颜色在安卓5
- 更改单选按钮的圆圈颜色
- 如何在android中复制一个文件?
- adb找不到我的设备/手机(MacOS X)