8

親レイアウトから子レイアウトにカスタム属性を渡そうとしています。

GetsStyledAttributes()から返された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
4

1 に答える 1