89

アプリケーション全体で広く使用するコントロール用のカスタム ウィジェットを作成しました。ImageButtonウィジェット クラスは、いくつかの簡単な方法で派生し、拡張します。使用時にウィジェットに適用できるスタイルを定義しましたが、これはテーマを通じて設定することをお勧めします。とR.styleableのようなウィジェット スタイル属性が表示されます。私が書いたカスタム ウィジェットにそのようなものを作成する方法はありますか?imageButtonStyletextViewStyle

4

2 に答える 2

211

はい、1 つの方法があります。

ウィジェットの属性の宣言があるとします ( 内attrs.xml):

<declare-styleable name="CustomImageButton">
    <attr name="customAttr" format="string"/>
</declare-styleable>

スタイル参照に使用する属性を宣言します ( 内attrs.xml):

<declare-styleable name="CustomTheme">
    <attr name="customImageButtonStyle" format="reference"/>
</declare-styleable>

ウィジェットのデフォルト属性値のセットを宣言します ( 内styles.xml):

<style name="Widget.ImageButton.Custom" parent="android:style/Widget.ImageButton">
    <item name="customAttr">some value</item>
</style>

カスタム テーマを宣言します ( 内themes.xml):

<style name="Theme.Custom" parent="@android:style/Theme">
    <item name="customImageButtonStyle">@style/Widget.ImageButton.Custom</item>
</style>

この属性をウィジェットのコンストラクター ( 内CustomImageButton.java)の 3 番目の引数として使用します。

public class CustomImageButton extends ImageButton {
    private String customAttr;

    public CustomImageButton( Context context ) {
        this( context, null );
    }

    public CustomImageButton( Context context, AttributeSet attrs ) {
        this( context, attrs, R.attr.customImageButtonStyle );
    }

    public CustomImageButton( Context context, AttributeSet attrs,
            int defStyle ) {
        super( context, attrs, defStyle );

        final TypedArray array = context.obtainStyledAttributes( attrs,
            R.styleable.CustomImageButton, defStyle,
            R.style.Widget_ImageButton_Custom ); // see below
        this.customAttr =
            array.getString( R.styleable.CustomImageButton_customAttr, "" );
        array.recycle();
    }
}

次に、(AndroidManifest.xml で)Theme.Customを使用するすべてのアクティビティに適用する必要があります。CustomImageButton

<activity android:name=".MyActivity" android:theme="@style/Theme.Custom"/>

それで全部です。現在のテーマの属性CustomImageButtonからデフォルトの属性値を読み込もうとします。customImageButtonStyleそのような属性がテーマまたは属性の値に見つからない場合は@null、最後の引数obtainStyledAttributesが使用されます:Widget.ImageButton.Customこの場合。

すべてのインスタンスとすべてのファイル (を除くAndroidManifest.xml) の名前を変更できますが、Android の命名規則を使用することをお勧めします。

于 2011-02-19T17:39:40.003 に答える
4

マイケルの優れた答えに加えて、テーマのカスタム属性をオーバーライドするもう1つの側面があります。すべてがカスタム属性「custom_background」を参照する多数のカスタム ビューがあるとします。

<declare-styleable name="MyCustomStylables">
    <attr name="custom_background" format="color"/>
</declare-styleable>

テーマでは、値が何であるかを定義します

<style name="MyColorfulTheme" parent="AppTheme">
    <item name="custom_background">#ff0000</item>
</style>

また

<style name="MyBoringTheme" parent="AppTheme">
    <item name="custom_background">#ffffff</item>
</style>

スタイルで属性を参照できます

<style name="MyDefaultLabelStyle" parent="AppTheme">
    <item name="android:background">?background_label</item>
</style>

のように Android 属性の参照にも使用される疑問符に注意してください。

?android:attr/colorBackground

ほとんどの人が気付いているように、ハードコーディングされた色の代わりに @color 参照を使用できます (おそらく使用する必要があります)。

では、なぜそうしないのですか

<item name="android:background">@color/my_background_color</item>

実行時に「my_background_color」の定義を変更することはできませんが、テーマは簡単に切り替えることができます。

于 2017-07-13T07:54:48.320 に答える