自定义xml属性始终返回0

时间:2016-04-12 13:44:25

标签: android xml fonts attributes styles

这是我的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。 谁能告诉我我做错了什么?

2 个答案:

答案 0 :(得分:0)

这是因为您要在样式之外声明属性。 将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>

答案 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 name应该是小写的。

相关问题