カスタム レイアウト コンポーネントのカスタム属性を問題なく使用しています。これまで、単純な属性 (string、int など) のみを使用してきました。これらは次のように定義されていvalues/attrs.xml
ます。
<declare-styleable name="StaticListView">
<attr name="border_size" format="dimension" />
</declare-styleable>
そして私のレイアウトでは:
<de.example.androidapp.StaticListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
namespace:border_size="1px"
/>
そして次のように使用されます:
int borderSize = (int) a.getDimension(R.styleable.StaticListView_border_size, 0);
現在、レイアウトをカスタム属性として指定しようとしていますが、R.styleable
上記の方法を使用できません。
属性を定義する方法は次のとおりです。
<declare-styleable name="StaticListView">
<attr name="emptyLayout" format="reference" />
</declare-styleable>
そしてそれを使用する:
<de.example.androidapp.StaticListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
namespace:emptyLayout="@layout/empty"
/>
そして、これは私がそれを使用したい方法ですが、私は常にデフォルト値(-1
)を取得します:
int emptyLayoutInt = attrs.getAttributeResourceValue(R.styleable.StaticListView_emptyLayout, -1);
ただし、これは機能します。
int emptyLayoutInt = attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/de.example.androidapp", "emptyLayout", -1);
XML 名前空間をハードコーディングする必要はありません。属性を使用するR.styleable
と、これをうまく回避できます。
私は何か間違ったことをしていますか、それともこれはバグ/予期される動作ですか?