0

我想为我的应用程序设置字体。字体就像“JOURNAL”。 但问题是,我不知道如何将它集成到我的应用程序中。如果我整合它,那么它会适用于所有应用程序还是仅适用于所选应用程序?因为我希望只为一个应用程序设置它。不是全部。 因此,我该怎么做?我看过here。但我认为它仅适用于TextView,而不适用于整个应用程序字体。 那么,有什么我必须做清单文件?或者我还有什么要做? 请帮助我。如何为Android集成字体时的问题应用程序

回答

2

我同意@Kheldar声明。没有方法可以在Android应用程序中更改字体。尝试使用此代码以避免每次要更改元素的字体时调用set方法。

public class MyTextView extends TextView { 

Context context; 
String ttfName; 

public MyTextView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    this.context = context; 

    for (int i = 0; i < attrs.getAttributeCount(); i++) { 
     this.ttfName = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.package.my", "ttf_name"); 

     init(); 
    } 
} 

private void init() { 
    Typeface font = Typeface.createFromAsset(context.getAssets(), ttfName); 
    setTypeface(font); 
} 

@Override 
public void setTypeface(Typeface tf) { 
    super.setTypeface(tf); 
} 

}

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:package="http://schemas.android.com/apk/res/com.package.my" 
    android:id="@+id/container" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <ImageView 
     android:id="@+id/icon" 
     android:layout_width="15dp" 
     android:layout_height="15dp" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:layout_marginRight="10dp" /> 
    <com.package.my.MyTextView 
     android:id="@+id/label" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_toRightOf="@+id/icon" 
     package:ttf_name="My-font.otf" /> 
</RelativeLayout> 
+0

感谢您的回复。但我把这个字体文件存入我的电脑。命名为“JOURNAL.ttf”Sowhere我应该把这个字体文件? –

+0

将其放入资产文件夹中。将其命名为小写,并更改xml文件以匹配文件名。 – nahwarang

+0

假设我有按钮在布局和按钮文本我想设置此字体然后?请帮助我解决这个问题。 –

1

您不能从应用程序修改系统范围的字体(至少据我所知并禁止某些漏洞利用)。

研究此链接:http://mobile.tutsplus.com/tutorials/android/customize-android-fonts/

+0

是感谢。它真的是一个很好的教程来帮助别人。 –

+0

我想要的帮助很少。如果我想在xml文件中设置字体呢?因为在教程中,演示是为了将java文件中的字体设置为不在xml文件中。 –

+0

如果每次我不想使用这两行设置字体,我该怎么办? –

相关问题