SORU
20 Aralık 2010, PAZARTESİ


Nasıl yapılır: tema (stil) özel widget için öğeyi Tanımlamak

Yaygın uygulama boyunca kullandığımız bir kontrol için özel bir widget yazdım. Widget sınıfı ImageButton türemiştir ve basit birkaç şekilde uzanır. Kullanılan olarak widget için geçerli bir stil tanımlanmış ettik, ama bu tema ile kurmak tercih ederim. R.styleable widget tarzı imageButtonStyle textViewStyle gibi nitelikler görüyorum. Yazdığım özel widget için böyle bir şey oluşturmak için herhangi bir yolu var mı?

CEVAP
19 ŞUBAT 2011, CUMARTESİ


Evet, bir yolu var:

Widget (attrs.xml): öznitelikleri ilanı olduğunu varsayalım

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

Stil başvuru (attrs.xml) için kullanacağınız bir nitelik ilan:

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

Widget için varsayılan özellik değerleri kümesi (styles.xml) beyan:

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

Özel bir tema (themes.xml) beyan:

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

Widget yapıcı (CustomImageButton.java): üçüncü bir argüman olarak, bu özelliği kullanın

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();
    }
}

Şimdi CustomImageButton (androidmanifest.xml) kullanan tüm etkinlikler için Theme.Custom uygulamak zorunda:

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

Hepsi bu. Şimdi CustomImageButton mevcut tema customImageButtonStyle öznitelik varsayılan öznitelik değerleri yüklemeye çalışır. Tema bulunur böyle bir öznitelik ya da öznitelik değeri obtainStyledAttributes için kullanılan son bağımsız olacak @null: bu durumda Widget.ImageButton.Custom.

Tüm örneklerini ve tüm dosyaların isimlerini değiştirebilirsiniz (AndroidManifest.xml hariç) ama daha iyi Android adlandırma kuralı kullanmak olacaktır.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Sergio Fernandez

    Sergio Ferna

    1 EKİM 2009
  • THELIFEOFPRICE

    THELIFEOFPRI

    16 Mart 2011
  • TokShogun

    TokShogun

    6 HAZİRAN 2009