2012-12-07 5 views
8

부모 레이아웃에서 자식 레이아웃으로 사용자 지정 특성을 전달하려고합니다.ObtainStyledAttributes를 사용하여 monodroid/xamarin 사용자 지정 특성이 비어 있습니다.

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이 같은 네임 스페이스를 정의 안드로이드 프로젝트에 대해 정의되어 있어야합니다. 또한

라인 :

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

나에게 약간 이상한보고 난 그냥 작성합니다

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

당신이 나중에 styleAttrs를 사용하지 않는 경우에 특히.

편집 : 안드로이드 SDK 이후 그것을 사용할 수 있습니다 (17) 레브 :

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

대신 전체 네임 스페이스를 작성하는 데.

관련 문제