2012-12-07 13 views
8

Próba przekazania niestandardowego atrybutu z układu nadrzędnego do układu podrzędnego.Atrybuty niestandardowe monodroid/xamarin są puste przy użyciu opcji ObtainStyledAttributes

TypedArray zwrócony z obiektu ObtainStyledAttributes() nie ma odpowiednich niestandardowych wartości dla niestandardowych właściwości, które utworzyłem, chociaż mogę odwzorować ich identyfikatory na wartości w Resource.designer.


Attr.xml:

<resources> 
<declare-styleable name="HeaderView"> 
    <attr name="bgcolor" format="color" /> 
    <attr name="testing" format="string" /> 
</declare-styleable> 

Main.xaml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:custom="http://schemas.android.com/apk/res"> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <views.HeaderView 
      android:id="@+id/hdrWatchList" 
      android:layout_width="fill_parent" 
      android:layout_height="20.0dp" 
      custom:bgcolor="@color/blue" 
      custom:testing="testing text buddy" /> 

Zobacz Klasa:

public HeaderView (Context context, IAttributeSet attrs) : 
     base (context, attrs) 
    { 
     int[] styleAttrs = Resource.Styleable.HeaderView; 
     TypedArray a = context.ObtainStyledAttributes(attrs, styleAttrs); 

     string sid = a.GetString(Resource.Styleable.HeaderView_testing); 
     int id = a.GetColor(Resource.Styleable.HeaderView_bgcolor, 555); 

     Log.Info("testing", "resource sid : " + sid); // RETURNS '' 
     Log.Info("testing", "resource id : " + id); // RETURNS DEF 555 

Odpowiedz

6

Myślę, że problem tkwi w jaki sposób określić swoją xmlns:custom przestrzeń nazw. Trzeba dodać nazw aplikacji na końcu łańcucha swoje już tak:

xmlns:custom="http://schemas.android.com/apk/res/my.awesome.namespace" 

Trzeba także mieć AndroidManifest.xml zdefiniowane dla projektu Android, gdzie zostały określone tak samo nazw.

Również linie:

int[] styleAttrs = Resource.Styleable.HeaderView; 
TypedArray a = context.ObtainStyledAttributes(attrs, styleAttrs); 

wyglądać nieco dziwne dla mnie, a ja po prostu napisać:

var a = context.ObtainStyledAttributes(attrs, Resource.Styleable.HeaderView); 

Zwłaszcza jeśli nie używasz styleAttrs później.

EDIT: ponieważ Android SDK rev 17 możliwe jest użycie:

xmlns:custom="http://schemas.android.com/apk/res-auto" 

zamiast napisać całą nazw.