2012-12-07 12 views
8

का उपयोग कर खाली हैं, माता-पिता लेआउट से एक कस्टम लेआउट में एक कस्टम लेआउट से पास करने का प्रयास कर रहे हैं।मोनोड्रॉइड/xamarin कस्टम विशेषताओं ObtainStyledAttributes

ObtainStyledAttributes() से लौटाया गया टाइपेडअरे, मेरे द्वारा बनाए गए कस्टम गुणों के लिए संबंधित कस्टम मान प्रतीत नहीं होता है, हालांकि मैं 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" /> 

देखें कक्षा:

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 

उत्तर

6

मुझे लगता है कि इस मुद्दे को कैसे आप अपने xmlns:custom निर्दिष्ट के साथ निहित है नाम स्थान। आप स्ट्रिंग के अंत में अपने अनुप्रयोगों के नाम स्थान जोड़ने की जरूरत है अपने पहले से ही ऐसा तरह है:

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

तुम भी एक AndroidManifest.xml अपने Android परियोजना है, जहां आप एक ही नाम स्थान में परिभाषित किया गया है परिभाषित करने की जरूरत है।

इसके अलावा लाइनों:

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

मेरे लिए थोड़ा अजीब देखो और मैं बस लिखना होगा:

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

खास तौर पर अगर आप styleAttrs पर बाद में उपयोग नहीं कर रहे।

संपादित करें: के बाद से Android SDK फिरना 17 के लिए इसका इस्तेमाल किया जा सकता है:

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

बजाय पूरे नाम स्थान में लिखने के लिए होने का।

संबंधित मुद्दे