我正在開發針對上述棒棒堂(Android 5.0)用戶的Android小部件應用程序。Android - 如何在xml中設置familiy字體「noto sans」?
由於某些原因,我必須在我的應用程序中使用名爲「noto sans」(https://www.google.com/get/noto/#/)的字體。
我聽說它包含在棒棒糖中,但我不知道如何在xml中使用它。
有人知道嗎?
我正在開發針對上述棒棒堂(Android 5.0)用戶的Android小部件應用程序。Android - 如何在xml中設置familiy字體「noto sans」?
由於某些原因,我必須在我的應用程序中使用名爲「noto sans」(https://www.google.com/get/noto/#/)的字體。
我聽說它包含在棒棒糖中,但我不知道如何在xml中使用它。
有人知道嗎?
不,您不能在佈局文件(XML)中使用自定義字體。您必須以編程方式使用自定義字體,如下所示:
String fontPath = "fonts/calibrib.ttf";
// Loading Font Face
Typeface tf = Typeface.createFromAsset(mContext.getAssets(), fontPath);
TextView txtView = new TextView(this);
txtView.setTypeface(tf);
將您的字體放置在資產文件夾中。這裏是上面的代碼工作。
你可以通過重寫這樣TextView的創建自己的TextView,你必須把字體的資產文件夾:
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setType(context);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setType(context);
}
public MyTextView(Context context) {
super(context);
setType(context);
}
private void setType(Context context){
this.setTypeface(Typeface.createFromAsset(context.getAssets(),
"foo.ttf"));
this.setShadowLayer(1.5f, 5, 5, getContext().getResources().getColor(R.color.black_shadow));
}
}
而且使用這樣的:
<com.your.project.package.MyTextView
android:id="@+id/oppinfo_mtv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="Player 1"
/>