我试图在Android Studio中使用自定义字体,就像我们在Eclipse中所做的那样。但不幸的是,不知道在哪里放“资产”文件夹!
当前回答
要在运行Android 4.1 (API级别16)及更高版本的设备上使用XML字体功能,请使用支持库26。有关使用支持库的更多信息,请参阅使用支持库一节。
添加字体作为资源,在Android Studio中执行以下步骤:
右键单击res文件夹,进入New > Android资源目录。 出现“新建资源目录”窗口。 在“资源类型”列表中选择字体,然后单击“确定”。 在字体文件夹中添加您的字体文件。
创建字体族 要创建字体族,请在Android Studio中执行以下步骤:
右键单击字体文件夹,选择新建>字体资源文件。出现“新建资源文件”窗口。 输入文件名,然后单击OK。新的字体资源XML在编辑器中打开。 在元素中包含每个字体文件、样式和权重属性。下面的XML演示了如何在字体资源XML中添加与字体相关的属性: 如果你的minSdkVersion是API级别26或更高
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
<font
android:fontStyle="normal"
android:fontWeight="400"
android:font="@font/lobster_regular" />
<font
android:fontStyle="italic"
android:fontWeight="400"
android:font="@font/lobster_italic" />
</font-family>
如果你的minSdkVersion低于API级别26
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
<font
app:font="@font/lobster_italic"
app:fontStyle="normal"
app:fontWeight="400" />
</font-family>
然后你可以像这样在任何地方使用它
android:fontFamily="@font/your_custom_font_file"
其他回答
Android 8.0 (API 26)引入了与字体相关的新功能。
1)字体可以作为资源使用。
2)可下载的字体。
如果你想在android应用程序中使用外部字体,你可以在apk中包含字体文件或配置可下载的字体。
在APK中包含字体文件:您可以下载字体文件,保存在res/字体文件器中,定义字体族,并在样式中使用字体族。
有关使用自定义字体作为资源的更多详细信息,请参阅http://www.zoftino.com/android-using-custom-fonts
配置可下载字体:通过提供字体提供程序详细信息来定义字体,添加字体提供程序证书并在样式中使用字体。
有关可下载字体的详细信息,请参阅http://www.zoftino.com/downloading-fonts-android
在Project -> app(或您的app名称)-> src -> main ->右键单击->新建->目录中创建文件夹资产。 然后在资产中创建一个名为fonts的新目录。
将字体分配给textView:
TextView textView = (TextView) findViewById(R.id.your_textView);
final Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/your_font_name");
Your_font_name包含字体扩展名。
我无法加载字体,因为我将字体文件命名为Poppins-Medium。Tff,重命名为poppins_medium。TFF对我很管用。 其余步骤保持不变:
在res文件夹下创建字体资源目录 将tff文件复制并粘贴到该目录中 然后在XML的TextView中使用fontFamily属性。 如果以上步骤不起作用,您可以使用此链接创建该字体的FontFamily
首先在字体文件夹中添加font.ttf文件。然后在onCreate方法中添加这一行
Typeface typeface = ResourcesCompat.getFont(getApplicationContext(), R.font.myfont);
mytextView.setTypeface(typeface);
这是我的xml
<TextView
android:id="@+id/idtext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="7dp"
android:gravity="center"
android:text="My Text"
android:textColor="#000"
android:textSize="10sp"
/>
对于新读者
你可以使用这个库 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加载JS包失败
- Android Studio, logcat在应用程序关闭后清理
- 在android中从上下文获取活动
- 无法解析主机"<URL here>"没有与主机名关联的地址
- getActivity()在Fragment函数中返回null
- 按钮背景是透明的
- 在Mac OS X上哪里安装Android SDK ?
- 我如何获得图像缩放功能?
- 在Android应用程序中显示当前时间和日期
- BottomSheetDialogFragment的圆角
- 在应用程序启动时出现“无法获得BatchedBridge,请确保您的bundle被正确打包”的错误
- 我如何改变默认对话框按钮的文本颜色在安卓5
- 更改单选按钮的圆圈颜色
- 如何在android中复制一个文件?
- adb找不到我的设备/手机(MacOS X)