2

通常、カスタム属性の例は次の形式です。

<declare-stylable name="MyView">
    <attr name="name" format="string"/>
</declare-styleable>  

とその使用法:

<com.example.test.MyView
    customns:name="Hello"/>

したがって、カスタム ビューには、スタイル可能な属性と同じ名前が付けられます。

しかし、この例 (クリックすると完全なコードが表示されます) では、次のようになります。

<declare-styleable name="Options">
    <attr name="titleText" format="string" localization="suggested" />
    <attr name="valueColor" format="color" />
</declare-styleable>

によって使われた:

<com.vogella.android.view.compoundview.ColorOptionsView
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    custom:titleText="Background color"
    custom:valueColor="@android:color/holo_green_light"
    />

ColorOptionsViewname で定義された属性にどのようにリンクされているのOptionsか疑問に思いました。

4

1 に答える 1

1

これらのオプションは、宣言されたnamespace customXMLの一部として利用でき、ファイルの先頭に含まれています。

xmlns:custom="http://schemas.android.com/apk/res/com.vogella.android.view.compoundview"

ノート

この行を追加するだけでは、オートコンプリートはサポートされません。それがあなたの質問の意味である場合は、ワークスペースのXML Catalogにスキーマを追加する必要があります。Eclipse でこれを行うには、 に移動してEclipse -> PreferencesからXML -> XML Catalog. ここで、Add...ボタンをクリックします。XML スキーマ ファイルに移動し、[ ] を選択しますOK。XML ファイルを閉じて再度開くと、オートコンプリートが表示されます。


最後に、 で使用されている属性をアンパックするColorOptionsView.javaと、作成者はこの名前空間から属性を明確に探すことができます。これは同じソースからのものです(私がコメントしました):

//grab the declared-styleable resource entries
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Options, 0, 0);
//get the "titleText" entry from this element's attributes
String titleText = a.getString(R.styleable.Options_titleText);
//get the "valueColor" attribute. If it does not exists, set the default to holo_blue_light
int valueColor = a.getColor(R.styleable.Options_valueColor,
    android.R.color.holo_blue_light);
a.recycle();
于 2013-04-22T18:27:03.440 に答える