1

カスタム レイアウト コンポーネントのカスタム属性を問題なく使用しています。これまで、単純な属性 (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と、これをうまく回避できます。

私は何か間違ったことをしていますか、それともこれはバグ/予期される動作ですか?

4

2 に答える 2

2

行を使用する代わりに:

int emptyLayoutInt = attrs.getAttributeResourceValue(R.styleable.StaticListView_emptyLayout, -1);

これを使って -

TypedArray a = context.obtainStyledAttributes(attrs,
            R.styleable.MyLayout);
    int layoutId = a.getResourceId(R.styleable.MyLayout_text,-1);

値が属性セットで使用できないため、-1 が返されます。

于 2012-07-20T09:53:34.877 に答える
0

私は自分の問題を理解しました。なんらかの理由でAttributeSet変数を調べていましたが、残りの属性と同じようにインスタンスを使用する必要がありました。動作するコード行は次のとおりです。attrsTypedArray

int emptyLayoutInt = a.getResourceId(R.styleable.StaticGridView_emptyLayout, -1);
于 2012-07-20T11:32:13.707 に答える