4

私は Android 開発の初心者です。現在、カスタム ビューを作成しようとしています。私は多くの問題に直面してきました。それらのいくつかは解決しましたが、最も理解しにくいのは属性です。属性を持つサンプルxmlファイルを見てみましょう

<declare-styleable name="ViewPagerIndicator">
        <!-- Style of the circle indicator. -->
        <attr name="vpiCirclePageIndicatorStyle" format="reference"/>
        <!-- Style of the icon indicator's views. -->
        <attr name="vpiIconPageIndicatorStyle" format="reference"/>
        <!-- Style of the line indicator. -->
        <attr name="vpiLinePageIndicatorStyle" format="reference"/>
        <!-- Style of the title indicator. -->
        <attr name="vpiTitlePageIndicatorStyle" format="reference"/>
        <!-- Style of the tab indicator's tabs. -->
        <attr name="vpiTabPageIndicatorStyle" format="reference"/>
        <!-- Style of the underline indicator. -->
        <attr name="vpiUnderlinePageIndicatorStyle" format="reference"/>
    </declare-styleable>
    <attr name="centered" format="boolean" />
    <attr name="selectedColor" format="color" />
    <attr name="strokeWidth" format="dimension" />
    <attr name="unselectedColor" format="color" />

    <declare-styleable name="CirclePageIndicator">
        <attr name="centered" />
        <attr name="fillColor" format="color" />
        <attr name="pageColor" format="color" />
        <attr name="android:orientation"/>
        <attr name="radius" format="dimension" />
        <attr name="snap" format="boolean" />
        <attr name="strokeColor" format="color" />
        <attr name="strokeWidth" />
        <attr name="android:background"/>
    </declare-styleable>
<declare-styleable name="LinePageIndicator">
    <attr name="centered" />
    <attr name="unselectedColor" />
    <attr name="selectedColor" />
    <attr name="lineWidth" format="dimension" />
    <attr name="strokeWidth" />
    <attr name="gapWidth" format="dimension" />
    <attr name="android:background"/>
</declare-styleable>
  1. 参照形式とは何を意味するのか、この形式が何を意味するのか、まだ理解できません。ドローアブルで使用できることがわかり、テーマで使用できることもわかりました。それが役立つ場所と、この形式タイプの主な目的を示す良い例を挙げてください。前の例ではスタイルに使用されていますが、カスタム ビューを使用する xml ファイルでこれを使用する方法がわかりません。
   <com.viewpagerindicator.CirclePageIndicator
        android:id="@+id/indicator"
        android:padding="10dip"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        custom:auto_cycle="true"
    custom:pageColor="@color/abc_background_cache_hint_selector_material_dark"
        />
  1. declare-styleable name="Name of custom view"私が理解しているように、各行は個別のカスタムビューに使用されています。これは例で確認できます。しかし、の目的は何<declare-styleable name="ViewPagerIndicator">ですか。このライブラリにはそのような名前のビューが見つかりませんでした。このスタイラブルには最初の質問 (参照属性) が含まれています。

  2. 属性が宣言スタイル可能外で宣言されている場合、それは複数のビューにあり、各ビューで共通の属性が外側に配置されているコードを複製しないことを意味します。私は正しいですか?

助けてください。チュートリアルやドキュメントを探すのに 1 日を費やしました。公式ドキュメントは非常に貧弱です。多くの初心者がこのトピックを説明してくれて感謝していると思います.

これを理解するのを手伝ってください。最も重要な質問は参照についてです(最初の質問) <attr name="vpiCirclePageIndicatorStyle" format="reference"/>これは何ですか、これをどのように使用するか、どこで宣言されていますか?

また、別のライブラリで使用されている別の例を見つけました

<declare-styleable name="Themes">
    <attr name="SliderStyle" format="reference"/>
    <attr name="PagerIndicatorStyle" format="reference"/>
</declare-styleable>

これがまったく理解できません。助けてください、あなたは私の最後の希望です。

4

1 に答える 1

6

私の知る限り、参照を使用すると、その属性で他のドローアブルとスタイルを参照できます。

<declare-styleable name="Theme">
    <attr name="indicatorStyle" format="reference"/>
</declare-styleable>

これにより、後で取得できるカスタム スタイルを宣言できます。

<style name="AppTheme.Platform.NoActionBar" parent="Theme.AppCompat.NoActionBar">
    <item name="indicatorStyle">@style/ViewPagerIndicator</item>
</style>

これを XML で使用できるようになりました

style="?attr/indicatorStyle"

この属性を宣言していないテーマで使用しようとすると、エラーが発生します。それViewPagerIndicatorが、アプリのテーマでカスタム属性を宣言するためのスタイルです。3 番目の質問はわかりませんでしたが、最初の 2 つはこれでカバーできるはずです。

于 2015-07-19T15:44:17.970 に答える