1

別のカスタムメイドのUIコンポーネント(便宜上分離されている)の中にあるカスタムメイドのUIコンポーネントがあります。

自分の属性を親コンポーネントに渡して、サブコンポーネント内でそれらを読み取れるようにしたいと思います。このように、開発者が確認する必要があるのは親コンポーネントだけであり、内部に別のコンポーネントがあることを知る必要はありません。

たとえば、これはアプリケーションのメインレイアウトです。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:udinic="http://schemas.android.com/apk/res/com.udinic"
android:id="@+id/main"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

    <com.udinic.FatherComponent android:id="@+id/fatherComp"
    android:layout_width="fill_parent"
    udinic:itemNum="9"
    android:layout_height="wrap_content" />

</LinearLayout>

父コンポーネントのxmlは次のようになります。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:udinic="http://schemas.android.com/apk/res/com.udinic"
android:id="@+id/fatherLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

    <com.udinic.SubComponent android:id="@+id/subComp"
    android:layout_width="fill_parent"
    udinic:itemNum=<<Get the itemNum passed to me>>
    android:layout_height="wrap_content" />

</LinearLayout>

XMLのみを使用してこれを行う方法は見つかりませんでした。誰かがこれを解決するのを助けることができる何かを知っていますか?

ありがとう

4

1 に答える 1

0

値をサブクラスに渡すには、親クラスのコンストラクターを使用する必要があります。

public FatherClass(Context context, AttributeSet attrs)
{
    super(context, attrs, LAYOUT);
    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomLayout);
    //for string attribute
    textView.setText(getStringAttribute(typedArray, R.styleable.CustomLayout_labelText));
    //for resources
    int textAppearance = typedArray.getResourceId(R.styleable.CustomLayout_textAppearance, -1);
    textView.setTextAppearance(context, textAppearance);
    //for integers
    int inputType = typedArray.getInt(R.styleable.CustomLayout_inputType, -1);
    editText.setInputType(inputType);
}
于 2013-10-10T20:34:12.553 に答える