在我的项目中,我想为应用程序使用Monsterrat字体,在某些屏幕上,我想使用Monster斜体,Monsterat中等字体。 我只想声明一次并在整个应用程序中使用它们。 我不知道该怎么办。
答案 0 :(得分:0)
使用此类
在您的xml
<your.class.path.RubikMediumTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Text"/>
并且java类是,并更改Typeface face= Typeface.createFromAsset(context.getAssets(), "font/rubik_medium.ttf");
中的字体
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class RubikMediumTextView extends TextView {
Typeface font;
public RubikMediumTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
public RubikMediumTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public RubikMediumTextView(Context context) {
super(context);
init(context);
}
public void init(Context context) {
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >=
android.os.Build.VERSION_CODES.KITKAT) {
if (!isInEditMode()) {
Typeface face= Typeface.createFromAsset(context.getAssets(), "font/rubik_medium.ttf");
setTypeface(face);
}
}else{
if (!isInEditMode()) {
Typeface face= Typeface.createFromAsset(context.getAssets(), "font/rubik_medium.ttf");
setTypeface(face);
}
}
}
}
答案 1 :(得分:0)
第一种方式:
将字体放入res / font目录并创建两种样式。 在此示例中,我还设置了文本颜色和文本大小:
<style name="FirstStyle" parent="Theme.AppCompat.Light.NoActionBar" >
<item name="android:fontFamily">@font/md_grotesk_regular</item>
<item name="fontFamily">@font/md_grotesk_regular</item>
<item name="android:textColor">@color/white</item>
<item name="android:textSize">@dimen/font_12</item>
</style>
然后为清单中的每个活动声明android:theme,如下所示:
<activity
android:name=".SomeActivity"
android:theme="@style/FirstStyle" />
这将为活动中定义的所有文本设置所选样式。
第二种方式:
查看@Sandeep Parish答案,并创建两个自定义textView,每个都有自己的样式,大小,颜色等。 调用来自定义每个TextView,以便可以在同一布局中使用两种样式(但这不是一种好习惯,除了某些类型的布局)
答案 2 :(得分:0)
使用res中的资产文件夹在您的应用程序中使用不同类型的字体。 在资源中添加ttf文件,然后继续。.