[英]Custom Font for an Android App
我试图在我的应用程序中添加自定义字体。 创建typeface
对象并将字体放入其中的方法有效。 现在,我想为自定义字体创建一个类,以使用更简洁的代码。
CustomFont.java
public class CustomFont extends AppCompatActivity {
private final Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Slabo.ttf");
public CustomFont(TextView textView) {
textView.setTypeface(typeface);
}
}
现在我正在尝试将此字体添加到textview
:
public class MainActivity extends AppCompatActivity {
private Typeface typeface;
private CustomFont customFont;
private TextView textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//typeface = Typeface.createFromAsset(getAssets(), "fonts/Slabo.ttf"); // works
textview = (TextView) findViewById(R.id.textviewTest);
//textview.setTypeface(typeface); // works
customFont = new CustomFont(textview); // does not work
}
}
但是,如果我运行此项目,则会出现以下异常:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.AssetManager android.content.Context.getAssets()' on a null object reference
尝试这个 :
TextView text = (TextView) findViewById(R.id.custom_font);
Typeface font = Typeface.createFromAsset(getAssets(), "yourfont.ttf");
text.setTypeface(font);
您可以在commonUtill类中制作如下所示的方法,以进行更好的说明
public static void setTypeface(Context mContext, View view, VIEW_TYPE type,
TYPE_FACE face, int bold) {
View mView = view;
Typeface tface = getTypeface(mContext, face);
switch (type) {
case TEXTVIEW:
((TextView) mView).setTypeface(tface, bold);
break;
case BUTTON:
((Button) mView).setTypeface(tface, bold);
break;
case EDITTEXT:
((EditText) mView).setTypeface(tface, bold);
break;
case RADIOBUTTON:
((RadioButton) mView).setTypeface(tface, bold);
break;
case CHECKBOX:
((CheckBox) mView).setTypeface(tface, bold);
break;
case CHECKEDTEXTVIEW:
((CheckedTextView) mView).setTypeface(tface, bold);
break;
default:
break;
}
}
并创建这样的功能
public static enum TYPE_FACE {
CALIBRI, ICON_FONT, BEBAS, AWESOME, BT, TAGLINE,
CALLIBRI, WEBLYSLEEK, WEBLYSLEEK_BOLD, ICON_FONT1
}
和您的查看方法。
public static enum VIEW_TYPE {
TEXTVIEW, BUTTON, EDITTEXT, RADIOBUTTON, CHECKBOX, CHECKEDTEXTVIEW
}
通过这种方式,您可以轻松地管理代码。
创建这样的Java
类。
public class TextViewKarlaBold extends TextView {
public TextViewKarlaBold(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public TextViewKarlaBold(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public TextViewKarlaBold(Context context) {
super(context);
init();
}
public void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"fonts/Karla-Bold.ttf");
setTypeface(tf, 1);
}
}
将此类用作XML
View
<hammerapps.views.TextViewKarlaBold // hammerapps.views is My Package name
android:id="@+id/txtVIEW"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="VIEW"
android:textColor="@color/black"
android:textSize="12dp" />
如果您希望整个应用程序使用特定字体, 请使用此库。
首先CustomFont
不应该是活动的,应该是普通班级。
public class CustomFont {
private Typeface typeface;
public CustomFont(Context context) {
typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Slabo.ttf");
}
public void setFont(TextView textView) {
textView.setTypeface(typeface);
}
}
并从您的MainActivity中调用它
customFont = new CustomFont(this);
customFont.setFont(textview);
Use Custom Textview
public class TextView extends android.widget.TextView {
Context mContext;
String str;
boolean isCorner;
//fonts
public static Typeface Font_name;
public TextView(Context context) {
super(context);
mContext=context;
initialiseFont(null);
}
public TextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext=context;
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TextView, 0, 0);
try {
str = ta.getString(R.styleable.TextView_font_family);
isCorner=ta.getBoolean(R.styleable.TextView_isCorner,false);
} finally {
ta.recycle();
}
initialiseFont(str);
}
private void initialiseFont(String font) {
if(font==null || font.equals("")){
}
else {
Font_name = Typeface.createFromAsset(mContext.getAssets(), "DroidSansFallbackanmol256.ttf");
setTypeface(Font_name);
}
}
使用attrs.xml
<declare-styleable name="TextView">
<attr name="font_family" format="string"/>
<attr name="isCorner" format="boolean"/>
</declare-styleable>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.