带有roboto字体的TextView示例:
attr.xml<?xml version="1.0"encoding="utf-8"?>
RobotoTextView.java:publicclassRobotoTextViewextendsTextView{/*
*Permissiblevaluesforthe"typeface"attribute.
*/privatefinalstaticintROBOTO_THIN=0;privatefinalstaticintROBOTO_THIN_ITALIC=1;privatefinalstaticintROBOTO_LIGHT=2;privatefinalstaticintROBOTO_LIGHT_ITALIC=3;privatefinalstaticintROBOTO_REGULAR=4;privatefinalstaticintROBOTO_ITALIC=5;privatefinalstaticintROBOTO_MEDIUM=6;privatefinalstaticintROBOTO_MEDIUM_ITALIC=7;privatefinalstaticintROBOTO_BOLD=8;privatefinalstaticintROBOTO_BOLD_ITALIC=9;privatefinalstaticintROBOTO_BLACK=10;privatefinalstaticintROBOTO_BLACK_ITALIC=11;privatefinalstaticintROBOTO_CONDENSED=12;privatefinalstaticintROBOTO_CONDENSED_ITALIC=13;privatefinalstaticintROBOTO_CONDENSED_BOLD=14;privatefinalstaticintROBOTO_CONDENSED_BOLD_ITALIC=15;/**
*Listofcreatedtypefacesforlaterreused.
*/privatefinalstaticSparseArraymTypefaces=newSparseArray(16);/**
*Simpleconstructortousewhencreatingaviewfromcode.
*
*@paramcontextTheContexttheviewisrunningin,throughwhichitcan
*accessthecurrenttheme,resources,etc.
*/publicRobotoTextView(Contextcontext){
super(context);}/**
*ConstructorthatiscalledwheninflatingaviewfromXML.Thisiscalled
*whenaviewisbeingconstructedfromanXMLfile,supplyingattributes
*thatwerespecifiedintheXMLfile.Thisversionusesadefaultstyleof
*0,sotheonlyattributevaluesappliedarethoseintheContext'sTheme
*andthegivenAttributeSet.
*
*
*ThemethodonFinishInflate()willbecalledafterallchildrenhavebeen
*added.
*
*@paramcontextTheContexttheviewisrunningin,throughwhichitcan
*accessthecurrenttheme,resources,etc.
*@paramattrsTheattributesoftheXMLtagthatisinflatingtheview.
*@see#RobotoTextView(Context,AttributeSet,int)
*/publicRobotoTextView(Contextcontext,AttributeSetattrs){
super(context,attrs);
parseAttributes(context,attrs);}/**
*PerforminflationfromXMLandapplyaclass-specificbasestyle.This
*constructorofViewallowssubclassestousetheirownbasestylewhen
*theyareinflating.
*
*@paramcontextTheContexttheviewisrunningin,throughwhichitcan
*accessthecurrenttheme,resources,etc.
*@paramattrsTheattributesoftheXMLtagthatisinflatingtheview.
*@paramdefStyleThedefaultstyletoapplytothisview.If0,nostyle
*willbeapplied(beyondwhatisincludedinthetheme).Thismay
*eitherbeanattributeresource,whosevaluewillberetrieved
*fromthecurrenttheme,oranexplicitstyleresource.
*@see#RobotoTextView(Context,AttributeSet)
*/publicRobotoTextView(Contextcontext,AttributeSetattrs,intdefStyle){
super(context,attrs,defStyle);
parseAttributes(context,attrs);}/**
*Parsetheattributes.
*
*@paramcontextTheContexttheviewisrunningin,throughwhichitcanaccessthecurrenttheme,resources,etc.
*@paramattrsTheattributesoftheXMLtagthatisinflatingtheview.
*/privatevoidparseAttributes(Contextcontext,AttributeSetattrs){
TypedArrayvalues=context.obtainStyledAttributes(attrs,R.styleable.RobotoTextView);
inttypefaceValue=values.getInt(R.styleable.RobotoTextView_typeface,0);
values.recycle();
setTypeface(obtaintTypeface(context,typefaceValue));}/**
*Obtaintypeface.
*
*@paramcontextTheContexttheviewisrunningin,throughwhichitcan
*accessthecurrenttheme,resources,etc.
*@paramtypefaceValuevaluesforthe"typeface"attribute
*@returnRoboto{@linkTypeface}
*@throwsIllegalArgumentExceptionifunknown`typeface`attributevalue.
*/privateTypefaceobtaintTypeface(Contextcontext,inttypefaceValue)throwsIllegalArgumentException{
Typefacetypeface=mTypefaces.get(typefaceValue);
if(typeface==null){
typeface=createTypeface(context,typefaceValue);
mTypefaces.put(typefaceValue,typeface);
}
returntypeface;}/**
*Createtypefacefromassets.
*
*@paramcontextTheContexttheviewisrunningin,throughwhichitcan
*accessthecurrenttheme,resources,etc.
*@paramtypefaceValuevaluesforthe"typeface"attribute
*@returnRoboto{@linkTypeface}
*@throwsIllegalArgumentExceptionifunknown`typeface`attributevalue.
*/privateTypefacecreateTypeface(Contextcontext,inttypefaceValue)throwsIllegalArgumentException{
Typefacetypeface;
switch(typefaceValue){
caseROBOTO_THIN:
typeface=Typeface.createFromAsset(context.getAssets(),"fonts/Roboto-Thin.ttf");
break;
caseROBOTO_THIN_ITALIC:
typeface=Typeface.createFromAsset(context.getAssets(),"fonts/Roboto-ThinItalic.ttf");
break;
caseROBOTO_LIGHT:
typeface=Typeface.createFromAsset(context.getAssets(),"fonts/Roboto-Light.ttf");
break;
caseROBOTO_LIGHT_ITALIC:
typeface=Typeface.createFromAsset(context.getAssets(),"fonts/Roboto-LightItalic.ttf");
break;
caseROBOTO_REGULAR:
typeface=Typeface.createFromAsset(context.getAssets(),"fonts/Roboto-Regular.ttf");
break;
caseROBOTO_ITALIC:
typeface=Typeface.createFromAsset(context.getAssets(),"fonts/Roboto-Italic.ttf");
break;
caseROBOTO_MEDIUM:
typeface=Typeface.createFromAsset(context.getAssets(),"fonts/Roboto-Medium.ttf");
break;
caseROBOTO_MEDIUM_ITALIC:
typeface=Typeface.createFromAsset(context.getAssets(),"fonts/Roboto-MediumItalic.ttf");
break;
caseROBOTO_BOLD:
typeface=Typeface.createFromAsset(context.getAssets(),"fonts/Roboto-Bold.ttf");
break;
caseROBOTO_BOLD_ITALIC:
typeface=Typeface.createFromAsset(context.getAssets(),"fonts/Roboto-BoldItalic.ttf");
break;
caseROBOTO_BLACK:
typeface=Typeface.createFromAsset(context.getAssets(),"fonts/Roboto-Black.ttf");
break;
caseROBOTO_BLACK_ITALIC:
typeface=Typeface.createFromAsset(context.getAssets(),"fonts/Roboto-BlackItalic.ttf");
break;
caseROBOTO_CONDENSED:
typeface=Typeface.createFromAsset(context.getAssets(),"fonts/Roboto-Condensed.ttf");
break;
caseROBOTO_CONDENSED_ITALIC:
typeface=Typeface.createFromAsset(context.getAssets(),"fonts/Roboto-CondensedItalic.ttf");
break;
caseROBOTO_CONDENSED_BOLD:
typeface=Typeface.createFromAsset(context.getAssets(),"fonts/Roboto-BoldCondensed.ttf");
break;
caseROBOTO_CONDENSED_BOLD_ITALIC:
typeface=Typeface.createFromAsset(context.getAssets(),"fonts/Roboto-BoldCondensedItalic.ttf");
break;
default:
thrownewIllegalArgumentException("Unknown`typeface`attributevalue"+typefaceValue);
}
returntypeface;}}
使用示例:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:typeface="roboto_thin"
android:textSize="22sp"
android:text="RobotoThin"/>