How to use Open Sans
font style for textview in android? by default in font-family Open Sans
is not available.
use this lib for font change on hole app Calligraphy
use this code for change perticuler text font.
put the font file in your assets folder. In my case I created a subdirectory called fonts.
TextView tv = (TextView) findViewById(R.id.textViewName);
Typeface face = Typeface.createFromAsset(getAssets(),"fonts/opansans.ttf");
tv.setTypeface(face);
Android O and Android Support Library 26 add support for Downloadable Fonts.
Google Fonts is shipping a Font Provider in Google Play Services.
Please read official doc: https://developers.google.com/fonts/docs/android
Step 1 : create an assest folder -> fonts folder -> place your .ttf file.
Step 2 : Create your custom textview class like below :
public class LotaRegularTextView extends TextView {
public LotaRegularTextView(Context context) {
super(context);
this.setTypeface(Typeface.SERIF);
}
public LotaRegularTextView(Context context, AttributeSet attrs) {
super(context, attrs);
this.setTypeface(Typeface.SERIF);
}
public LotaRegularTextView(Context context, AttributeSet attrs, int
defStyleAttr) {
super(context, attrs, defStyleAttr);
this.setTypeface(Typeface.SERIF);
}
}
Step 3 : Add FontsOverride.class to your package this class replace default font family to your font family
public final class FontsOverride {
public static void setDefaultFont(Context context,
String staticTypefaceFieldName, String fontAssetName) {
final Typeface regular = Typeface.createFromAsset(context.getAssets(),
fontAssetName);
replaceFont(staticTypefaceFieldName, regular);
}
protected static void replaceFont(String staticTypefaceFieldName,
final Typeface newTypeface) {
try {
final Field staticField = Typeface.class
.getDeclaredField(staticTypefaceFieldName);
staticField.setAccessible(true);
staticField.set(null, newTypeface);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
Step 4 : Write line in application class on create method. that replace "serif" font to your font family
FontsOverride.setDefaultFont(this, "SERIF", "fonts/Lato-Regular.ttf");
Step 5 : How can use custom textview class like below
<com.example.widget.LotaRegularTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/padding10"
android:text="Ashish"
android:textColor="@color/gini_gray_color_7d7d7d"
android:textSize="@dimen/s_common_a"/>
您可以检查答案https://stackoverflow.com/a/3651168/6792992您可以按照与建议答案相同的方式使用任何一种字体
You must add a custom font. First download the font archive .ttf which in your case can be found at : https://www.fontsquirrel.com/fonts/open-sans once you have it I recommend you this tutorial to add custom fonts: https://futurestud.io/tutorials/custom-fonts-on-android-using-font-styles
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.