कस्टम फ़ॉन्ट लागू करने के लिए
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FontManager.getInstance().initialize(this, R.xml.fonts);
setContentView(R.layout.main);
}
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
SimpleFactory factory = new SimpleFactory();
return factory.onCreateView(name, context, attrs);
}
जहां FontManager इस वर्ग कि सभी फ़ॉन्ट्स कि /xml/fonts.xml और SimpleFactory सिर्फ कारखाना है कि दृश्य बनाने और प्रत्येक के लिए कस्टम फ़ॉन्ट लागू में परिभाषित का प्रबंधन कि उदाहरण देखने जाता है: सभी आवेदन भर में, बस नीचे दिए गतिविधि बनाने पाठ दृश्य का।
/xml/fonts.xml
<?xml version="1.0" encoding="utf-8"?>
<familyset>
<family>
<nameset>
<!--Font name-->
<name>HelveticaNeueLTStd</name>
</nameset>
<fileset>
<!--Font styles-->
<file style="normal">fonts/HelveticaNeueLTStd-LtCn.otf</file>
<file style="bold">fonts/HelveticaNeueLTStd-MdCn.otf</file>
<file style="italic">fonts/HelveticaNeueLTStd-LtCnO.otf</file>
<file style="bold_italic">fonts/HelveticaNeueLTStd-MdCnO.otf</file>
</fileset>
</family>
<family>
<!--There new font family can be added,
don't forget add font files into /assets/fonts directory and
put the name of the font into /values/string/font.xml-->
</family>
</familyset>
FontFactory - बस/xml/फोंट में परिभाषित फ़ॉन्ट फ़ाइलों को मैप - abastract वर्ग, बस अपने खुद के कारखाने
public abstract class FontFactory implements LayoutInflater.Factory{
public final String TAG = getClass().getSimpleName();
static final Class<?>[] mConstructorSignature = new Class[] {Context.class, AttributeSet.class};
final Object[] mConstructorArgs = new Object[2];
private static final String[] sClassPrefixList = {
"android.widget.",
"android.webkit."
};
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
if("ViewStub".equals(name) || "View".equals(name)){
return null;
}
View view = null;
Constructor<? extends View> constructor = null;
Class clazz = null;
if (view == null) {
if (-1 == name.indexOf('.')) {
for (String prefix : sClassPrefixList) {
clazz = getClazz(prefix, name);
if(clazz != null){
break;
}
}
} else {
clazz = getClazz("", name);
}
}
if (clazz == null) {
Log.d(TAG, "View can't be created " + name);
return null;
}
try {
constructor = clazz.getConstructor(mConstructorSignature);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
Object[] args = mConstructorArgs;
args[1] = attrs;
if(constructor == null){
return null;
}
try {
view = constructor.newInstance(context, attrs);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
if(view != null){
onFontApply(context, view);
}
return view;
}
public abstract void onFontApply(Context context, View view);
private Class getClazz(String prefix, String name){
Class clazz = null;
try {
clazz = Class.forName(prefix + name);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
return clazz;
}
}
}
FontManager बनाने के लिए इसका फैली हुई है। एक्सएमएल फ़ॉन्ट फ़ाइलों को/संपत्ति निर्देशिका में रखा गया है और फ़ॉन्ट परिवार नाम और फ़ॉन्ट शैली द्वारा टाइपफेस लौटाएं।
public void initialize(Context context, int resId) {
if(mFonts != null){
Log.d(TAG,"FontManager have already initialized");
return;
}
XmlResourceParser parser = null;
try {
parser = context.getResources().getXml(resId);
mFonts = new ArrayList<Font>();
String tag;
String fontStryleAttr = null;
int eventType = parser.getEventType();
Font font = null;
do {
tag = parser.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
if (tag.equals(TAG_FAMILY)) {
// one of the font-families.
font = new Font();
} else if (tag.equals(TAG_NAMESET)) {
// a list of font-family names supported.
font.families = new ArrayList<String>();
} else if (tag.equals(TAG_NAME)) {
isName = true;
} else if (tag.equals(TAG_FILESET)) {
// a list of files specifying the different styles.
font.styles = new ArrayList<FontStyle>();
} else if (tag.equals(TAG_FILE)) {
isFile = true;
fontStryleAttr = parser.getAttributeValue(null, ATTR_STYLE);
}
break;
case XmlPullParser.END_TAG:
if (tag.equals(TAG_FAMILY)) {
// add it to the list.
if (font != null) {
mFonts.add(font);
font = null;
}
} else if (tag.equals(TAG_NAME)) {
isName = false;
} else if (tag.equals(TAG_FILE)) {
isFile = false;
fontStryleAttr = null;
}
break;
case XmlPullParser.TEXT:
String text = parser.getText();
if (isName) {
// value is a name, add it to list of family-names.
if (font.families != null)
font.families.add(text);
} else if (isFile) {
// value is a file, add it to the proper kind.
FontStyle fontStyle = new FontStyle();
fontStyle.font = Typeface.createFromAsset(context.getAssets(), text);
String attr = parser.getAttributeValue(null, ATTR_STYLE);
if (fontStryleAttr.equals(STYLE_BOLD))
fontStyle.style = Typeface.BOLD;
else if (fontStryleAttr.equals(STYLE_ITALIC))
fontStyle.style = Typeface.ITALIC;
else if (fontStryleAttr.equals(STYLE_BOLD_ITALIC))
fontStyle.style = Typeface.BOLD_ITALIC;
else
fontStyle.style = Typeface.NORMAL;
font.styles.add(fontStyle);
}
}
eventType = parser.next();
} while (eventType != XmlPullParser.END_DOCUMENT);
} catch (XmlPullParserException e) {
throw new InflateException("Error inflating font XML", e);
} catch (IOException e) {
throw new InflateException("Error inflating font XML", e);
} finally {
if (parser != null)
parser.close();
}
}
public Typeface get(String family, int style) {
for (Font font: mFonts) {
for (String familyName : font.families) {
if (familyName.equals(family)) {
// if no style in specified, return normal style.
if (style == -1)
style = Typeface.NORMAL;
for (FontStyle fontStyle : font.styles) {
if (fontStyle.style == style)
return fontStyle.font;
}
}
}
}
return mDefaultFont;
}
अधिक कोड और नमूने के लिए
सिर्फ देखने के here
जाएँ इस एक: हो सकता है कि यह आप के लिए उपयोगी हो सकते हैं: http://stackoverflow.com/questions/2888508/कैसे-टू-चेंज-द-फ़ॉन्ट-ऑन-द-टेक्स्टव्यू/16166184 # 16166184 –
नहीं, कोई रास्ता नहीं है। आपको अपने फ़ॉन्ट को बंडल करना होगा और जैसा कि निक कहते हैं। आप इस धागे को भी देख सकते हैं [http://stackoverflow.com/questions/2376250/custom-fonts-and-xml-layouts-android ](httpoverstow.com/questions/2376250/custom-fonts-and -xml-layouts-android) –
किसी को भी एसी #/एक्समरिन समाधान की आवश्यकता है जो टेक्स्टव्यू बढ़ाता है लेकिन यह सामान्य बनाता है यानी आप xml पर प्रत्येक टेक्स्टव्यू पर एक विशेषता के रूप में एक विशिष्ट फ़ॉन्ट पथ पारित कर सकते हैं, फिर यहां एक नज़र डालें https: //github.com/Cheesebaron/Cheesebaron.FontSample –