我试图在Android Studio中使用自定义字体,就像我们在Eclipse中所做的那样。但不幸的是,不知道在哪里放“资产”文件夹!
当前回答
对于新读者
你可以使用这个库 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非常陌生,这可能有点棘手。请务必致电:
TextView myTextView = (TextView) findViewById(R.id.textView);
Typeface typeface=Typeface.createFromAsset(getAssets(), "fonts/your font.ttf");
myTextView.setTypeface(typeface);
方法,例如onCreate。
我认为我们可以使用谷歌字体而不是下载。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 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"
对于新读者
你可以使用这个库 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" />
现在有很多应用字体的方法,最简单的方法之一是这样的, 1)右键单击res文件夹 进入“新建> Android资源目录”。
2)从资源类型列表中选择字体,然后单击确定。
3)把你的字体文件放在字体文件夹里。
推荐文章
- 如何允许所有网络连接类型HTTP和HTTPS在Android(9)馅饼?
- Android加载JS包失败
- Android Studio, logcat在应用程序关闭后清理
- 在android中从上下文获取活动
- 无法解析主机"<URL here>"没有与主机名关联的地址
- getActivity()在Fragment函数中返回null
- 按钮背景是透明的
- 在Mac OS X上哪里安装Android SDK ?
- 我如何获得图像缩放功能?
- 在Android应用程序中显示当前时间和日期
- BottomSheetDialogFragment的圆角
- 在应用程序启动时出现“无法获得BatchedBridge,请确保您的bundle被正确打包”的错误
- 我如何改变默认对话框按钮的文本颜色在安卓5
- 更改单选按钮的圆圈颜色
- 如何在android中复制一个文件?