Загружаемые шрифты для Android с использованием полужирного стиля
Я использую загружаемые шрифты Google, как указано в ссылке ниже
https://developer.android.com/guide/topics/ui/look-and-feel/downloadable-fonts.html
Я хочу использовать стиль шрифта Montserrat полужирный, но этот вариант не существует в Android
Также в ссылке ниже есть стиль полужирный любой идеи, как это сделать
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" />
3 ответа
Вы можете скачать здесь все типы шрифтов 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"
Надеюсь, поможет
Вы можете создать новый шрифт в res/font
:
<?xml version="1.0" encoding="utf-8"?>
<font-family
xmlns:app="http://schemas.android.com/apk/res-auto"
app:fontProviderAuthority="com.google.android.gms.fonts"
app:fontProviderPackage="com.google.android.gms"
app:fontProviderQuery="name=Montserrat&weight=600"
app:fontProviderCerts="@array/com_google_android_gms_fonts_certs">
</font-family>
Вы можете указать любой поддерживаемый вес, изменив weight
внутри app:fontProviderQuery
(600
для полужирного шрифта). Android Studio подниметFontValidationWarning
, но тем не менее он будет работать.
Источник: Документация Google Fonts API для Android.
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
Попробуйте, создайте стиль шрифта Customize для приложения Android,
сначала создать assets
папку затем создать внутри этого font
затем вставьте загруженный шрифт (например, Light.ttf), введите описание ссылки здесь
Затем добавьте эту строку в активность
Typeface mycustomface1 = Typeface.createFromAsset(context.getAssets(), "fonts/Light.ttf");
затем объявите, что просмотр текста
textview.setTypeface(mycustomface1);