我正在使用谷歌可下载字体,如下面的链接所示
https://developer.android.com/guide/topics/ui/look-and-feel/downloadable-fonts.html
我想使用蒙特塞拉特字体样式半粗体,但该选项不在android中
同样在下面的链接有风格semibold任何想法如何做到这一点
https://fonts.google.com/specimen/Montserrat
以下是我添加字体系列后的textview xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:fontFamily="@font/montserrat"
android:text="@string/address"
android:textAllCaps="false"
android:textColor="@color/title_light_color"
android:textSize="10sp"
android:textStyle="bold" />
你可以在这里下载所有类型的字体montserrat
https://github.com/google/fonts/tree/master/ofl/montserrat
https://github.com/google/fonts/blob/master/ofl/montserrat/Montserrat-SemiBold.ttf
添加你的res/font
文件夹使用它
android:fontFamily="@font/Montserrat-SemiBold"
希望能帮助到你
Create a class Name FontCache
import android.content.Context;
import android.graphics.Typeface;
import java.util.Hashtable;
public class FontCache
{
private static Hashtable<String, Typeface> fontCache = new Hashtable<>();
public static Typeface get(String name, Context context)
{
Typeface tf = fontCache.get(name);
if(tf == null)
{
try
{
tf = Typeface.createFromAsset(context.getAssets(), name);
}
catch (Exception e)
{
return null;
}
fontCache.put(name, tf);
}
return tf;
}}
Then put the below lines of code in your activity.
Typeface montserratFontType = FontCache.get("fonts/montserrat.ttf",MainActivity.this);
yourtextview.setTypeface(montserratFontType);
At last create a "assests" folder inside your main folder then create
a "fonts" folder inside a "assests "folder and put your montserrat.ttf
file
试试这个,为Android应用程序创建自定义字体样式,
首先创建assets
文件夹然后在font
内创建然后放下你下载的字体(例如:Light.ttf)enter link description here
然后在活动中添加此行
Typeface mycustomface1 = Typeface.createFromAsset(context.getAssets(), "fonts/Light.ttf");
然后声明textview
textview.setTypeface(mycustomface1);