[英]How to give Bungee font style to textview in android?
我想在我的android xml代碼中使用蹦極嵌入式字體樣式
<RelativeLayout
android:id="@+id/sound_setting_layout"
android:layout_width="500dip"
android:layout_height="350dip"
android:layout_marginTop="65dip"
android:layout_marginLeft="780dip"
android:layout_alignParentTop="true"
android:padding="10dip"
android:gravity="center"
android:visibility="gone"
android:background="@drawable/volume_layout"
>
<TextView
android:layout_width="450dip"
android:layout_height="50dip"
android:gravity="center_horizontal"
android:layout_alignParentTop="true"
android:text="Volume Control"
android:textStyle="bold"
android:textColor="#ffffff"
android:textSize="30dip"
/>
我嘗試了很多,但是在Android中找不到字體Bungee。
將字體文件加載到資產文件夾,然后在您的活動onCreate中,使用以下方法
Typeface face = Typeface.createFromAsset(YOUR_ACTIVITY.this.getAssets(),"fonts/YOUR_FONT_FILE_NAME.otf");
your_text_view.setTypeface(face);
我們在android中沒有默認的橡皮筋字體樣式,因此,如果您想使用它,請下載橡皮筋.ttf文件,並在名為fonts的 資產中創建一個文件夾,然后將下載的字體(.ttf)粘貼到此處,您可以在此處下載Bungee字體: https: //djr.com/bungee/在您的代碼中執行此操作
// Font path insted of bungee.ttf replace your .ttf file
String fontPath = "fonts/bungee.ttf";
// text view label which you want to apply Bungee font
TextView txtGhost = (TextView) findViewById(R.id.androidSample);
// here loading Font Face
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
// Applying font
txtGhost.setTypeface(tf);
如果要在整個應用程序中使用自定義字體,例如在多個TextView上,最好使用Singleton模式,因為一遍又一遍地重新設置字體會降低應用程序的速度。
嘗試此類,並用您自己的自定義字體替換字體路徑,確保在“ main”中的“ assets”文件夾中包含自定義字體
public class ProximaTypeface {
public static ProximaTypeface instance = new ProximaTypeface();
public ProximaTypeface() {
}
public Typeface regularTypeFace = null;
public Typeface semiBoldTypeFace = null;
public static ProximaTypeface getInstance() {
return instance;
}
public void getRegularTypeface(Context context, TextView textView) {
if (regularTypeFace == null) {
regularTypeFace = Typeface.createFromAsset(context.getResources().getAssets(), "fonts/proxima_nova_regular.otf");
}
textView.setTypeface(regularTypeFace);
}
public void getSemiBoldTypeface(Context context, TextView textView) {
if (semiBoldTypeFace == null) {
semiBoldTypeFace = Typeface.createFromAsset(context.getResources().getAssets(), "fonts/proxima_nova.otf");
}
textView.setTypeface(semiBoldTypeFace);
}
}
然后在您的活動中:
ProximaTypeface proximaTypeface = new ProximaTypeface();
TextView myTextView = (TextView) findViewById(R.id.textView);
proximaTypeface.getRegularTypeface(context,myTextView);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.