2016-04-12 9 views
1

Das ist mein attrs.xml Dateibenutzerdefinierte XML-Attribute zurückgeben immer 0

<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> 

Diese Datei mein Layout ist

<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> 

Und das ist meine customtextview Klasse

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(); 
    } 


} 

Diese Linie values.getInt (R.styleable.MyCustomTextView_fontName, 1) gibt immer 0 zurück. Kann mir jemand sagen, was ich falsch mache?

Antwort

0

schließlich bekam ich meine Arbeitslösung, indem Sie diese Zeile

xmlns:app="http://schemas.android.com/apk/test.bpl.com.bploximeterdemo" 
to 
xmlns:app="http://schemas.android.com/apk/res-auto" 

Und

<attr name="fontName" format="enum"> to <attr name="fontname" format="enum"> 

attr name smallcase werden sollte.

0

Dies liegt daran, dass Sie Ihr Attribut außerhalb des Styleable deklarieren. Ändern Ihre attrs.xml dazu:

<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> 
+0

gleiche Sache happening.it 0 zurück – abh22ishek

+0

ya wird elbe – abh22ishek

+0

auch geschieht, Ihre App-Namespace 'Xmlns ändern: app = "http://schemas.android.com/apk/res-auto"' –