这是我的attrs.xml文件
<declare-styleable name="MyCustomTextView">
<attr name="fontName" />
</declare-styleable>
<attr name="fontName">
<enum name="centralSansXBold" value="1" />
<enum name="centralSansLight" value="2" />
<enum name="centralSansBook" value="3" />
<enum name="avantgarde" value="4" />
<enum name="avangami" value="5" />
</attr>
这是我的布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/test.bpl.com.bploximeterdemo"
android:layout_width="match_parent"
android:id="@+id/sourcepan"
android:layout_height="match_parent">
<wl.com.bpl.customviews.MyCustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text="679"
android:textSize="@dimen/pi_text_size"
android:layout_marginLeft="40dp"
app:fontName="avangami"
android:id="@+id/perfusion_index"
android:gravity="center"
android:layout_marginStart="38dp"
android:layout_centerVertical="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
这是我的customtextview类
public class MyCustomTextView extends TextView{
private final static int CENTRALESANS_XBOLD = 1;
private final static int CENTRALSANS_LIGHT = 2;
private final static int CENTRALSANS_BOOK = 3;
private final static int AVANT_GARDE=4;
private final static int AVANT_GAMI=5;
String TYPEFACE_CENTRALSANS_XBOLD = "fonts/CentraleSans-XBold.otf";
String TYPEFACE_CENTRALSANS_Light = "fonts/CentraleSans-Light.otf";
String TYPEFACE_CENTRALSANS_BOOK = "fonts/CentraleSans-Book.otf";
String TYPEFACE_AVANT_GARDE="fonts/ufonts.com_avantgarde.ttf";
String TYPEFACE_AVANT_GAMI= "fonts/avangami.ttf";
String TAG=MyCustomTextView.class.getSimpleName();
public MyCustomTextView(Context context) {
super(context);
}
public MyCustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
parseAttributes(context,attrs);
}
public MyCustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
parseAttributes(context,attrs);
}
private void parseAttributes(Context context, AttributeSet attrs) {
TypedArray values = context.obtainStyledAttributes(attrs, R.styleable.MyCustomTextView);
int typeface = values.getInt(R.styleable.MyCustomTextView_fontName,1);
//typeface always return 0
Logger.log(Level.INFO,TAG,"styleable typeface value="+R.styleable.MyCustomTextView_fontName);
switch (typeface) {
case CENTRALESANS_XBOLD:
//Typeface centralSansXBold = Typeface.createFromAsset(context.getAssets(), "fonts/CentraleSans-XBold.otf");
Typeface centralSansXBold=FontTypeFace.getTypeFace(TYPEFACE_CENTRALSANS_XBOLD, context);
setTypeface(centralSansXBold);
break;
case CENTRALSANS_LIGHT:
Typeface centralSansLight=FontTypeFace.getTypeFace(TYPEFACE_CENTRALSANS_Light, context);
setTypeface(centralSansLight);
break;
case CENTRALSANS_BOOK:
Typeface centralSansBook = FontTypeFace.getTypeFace(TYPEFACE_CENTRALSANS_BOOK, context);
setTypeface(centralSansBook);
break;
case AVANT_GARDE:
Typeface avantgarde=FontTypeFace.getTypeFace(TYPEFACE_AVANT_GARDE, context);
setTypeface(avantgarde);
break;
case AVANT_GAMI:
Typeface avangami=FontTypeFace.getTypeFace(TYPEFACE_AVANT_GAMI, context);
setTypeface(avangami);
break;
}
values.recycle();
}
}
这一行values.getInt(R.styleable.MyCustomTextView_fontName,1)始终返回0。有人能告诉我我做错了什么吗?
通过更改这一行,我终于得到了我的工作解决方案
xmlns:app="http://schemas.android.com/apk/test.bpl.com.bploximeterdemo"
to
xmlns:app="http://schemas.android.com/apk/res-auto"
而且,
<attr name="fontName" format="enum"> to <attr name="fontname" format="enum">
attr名称应为小写。
这是因为您在样式表之外声明属性。将您的attrs.xml更改为:
<declare-styleable name="MyCustomTextView">
<attr name="fontName" format="enum">
<enum name="centralSansXBold" value="1" />
<enum name="centralSansLight" value="2" />
<enum name="centralSansBook" value="3" />
<enum name="avantgarde" value="4" />
<enum name="avangami" value="5" />
</attr>
</declare-styleable>