2012-12-07 12 views
8

親レイアウトから子レイアウトにカスタム属性を渡そうとしています。Montainroid/xamarinカスタム属性は、ObtainStyledAttributesを使用して空です。

ObtainStyledAttributes()から返されたTypedArrayは、作成したカスタムプロパティの対応するカスタム値を持っていないようですが、IDを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を使用していない場合は特に。

EDIT:AndroidのSDK以来はそれを使用することが可能である17を吹け:

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

全体ではなく、名前空間を記述することの。

関連する問題