186

カスタム ウィジェットを作成し、layout.xml で宣言しています。また、attr.xml にいくつかのカスタム属性を追加しました。ただし、styles.xml のスタイルでこれらの属性を宣言しようとすると、No resource found that matches the given name: attr 'custom:attribute'.

、、およびxmlns:custom="http://schemas.android.com/apk/res/com.my.package"を含むすべてのタグを styles.xml に挿入しましたが、カスタム XML 名前空間が見つからないという同じエラーが表示されます。<?xml><resources><style>

ただし、名前空間を使用して、layout.xml のビューに属性を手動で割り当てることができるので、名前空間に問題はありません。私の問題は、styles.xml に私の attr.xml を認識させることにあります。

4

6 に答える 6

368

私はそれを考え出した!答えは、スタイルで名前空間を指定しないことです。

<?xml version="1.0" encoding="utf-8" ?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="CustomStyle">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>

        <item name="custom_attr">value</item> <!-- tee hee -->
    </style>
</resources>
于 2011-07-29T03:23:46.743 に答える
44

上記の答えはうまくいきました。ちょっとした変更を試みました。リソース要素のクラスのスタイルを宣言します。

<declare-styleable name="VerticalView">
    <attr name="textSize" format="dimension" />
    <attr name="textColor" format="color" />
    <attr name="textBold" format="boolean" />
</declare-styleable>

declare-styleableでは、name属性がクラス名を参照していたので、ビュー クラス コール「com.my.package.name.VerticalView」がありました。これは、この宣言を VerticalView または VerticalView のサブクラスで使用する必要があることを表しています。したがって、次のようにスタイルを宣言できます。

<resources>
    <style name="verticalViewStyle">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">36dip</item>

        <item name="textSize">28sp</item>  <!-- not namespace prefix -->
        <item name="textColor">#ff666666</item>
        <item name="textBold">true</item>
    </style>
</resources>

そのため、resources 要素で名前空間を宣言しませんでしたが、それでも機能します。

于 2013-05-07T07:23:04.297 に答える
10

Styler と vince の修正がうまくいきました。@vince の説明が完全に正確ではない可能性があることを指摘したいと思います。

declare-styleableカスタム ビュー クラスの名前と一致する名前属性により、名前空間なしでカスタム属性にアクセスできるという仮説をテストするために、名前を変更しましたdeclare-styleable(カスタム ビューの名前はTestViewFont:

<declare-styleable name="TextViewFont2">
    <attr name="font" format="integer"/>
</declare-styleable>

次に、obtainStyledAttributesこれを反映するようにカスタム ビューの呼び出しを変更しました。

TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.TextViewFont2, 0, 0);

コードはまだ実行されました。declare-styleableですから、それが名前の由来となったクラスによるある種の内省ではないと思います。

したがって、カスタム属性を使用して、名前空間を参照せずにスタイルを宣言できると信じるようになりました。

とにかく、すべての助けてくれてありがとう、それは私の問題を解決しました.

于 2013-10-30T16:15:00.710 に答える
2

それが他の誰かを助ける場合、私の間違いは、私のカスタムビュークラスがAttributeSet.getAttributeValueを呼び出していたことです。

String fontName = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto", "customFont");

...その結果、カスタム ビューのカスタム属性が読み込まれませんでした。

修正はobtainStyledAttributes、カスタム ビューで使用することでした。

 TypedArray styleAttrs = context.obtainStyledAttributes(attrs, R.styleable.MyTextViewStyleable);
 String fontName = styleAttrs.getString(R.styleable.MyTextViewStyleable_customFont);

これが正しく機能していることのヒントは、Ctrl/Apple + をクリックしR.styleable.MyTextViewStyleable_customFontて attrs.xml 定義に直接移動できることです。

カスタム属性は (スタイルではなく) レイアウト XML を介して直接渡された場合に正常に機能したため、私のコードと他の例とのこの重大な違いを見つけるのにしばらく時間がかかりました。

于 2014-12-13T02:52:09.367 に答える