SORU
11 Mart 2010, PERŞEMBE


Nasıl AlertDialog için tema değiştirmek için

Eğer birisi bana yardımcı olabilir merak ediyordum. Özel bir AlertDialog oluşturmak için çalışıyorum. Bunu yapmak için, styles.xml kod aşağıdaki satırı ekledim

@/color_panel_background drawable

  • color_panel_background.9.png drawable klasöründe bulunur. Bu da Android SDK res klasöründe kullanılabilir.

Aşağıdaki ana faaliyettir.

package com.customdialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;

public class CustomDialog extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        this.setTheme(R.style.CustomAlertDialog);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("HELLO!");
        builder .setCancelable(false)
          .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               //MyActivity.this.finish();
           }
       })
       .setNegativeButton("No", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               //dialog.cancel();
           }
       });

        AlertDialog alertdialog = builder.create();
        alertdialog.show();
    }
}

Bir AlertDialog için tema uygulamak için, geçerli bağlam için tema ayarlamak zorunda kaldım.

Ancak, sadece app özelleştirilmiş AlertDialog göstermek için almak gibi olamaz. Herkes bu konuda bana yardımcı olabilir, ve şimdiden çok teşekkür ederim!

CEVAP
6 EKİM 2010, ÇARŞAMBA


Dialog.java (Android src) ContextThemeWrapper kullanılır. Fikri kopyalama gibi ve bir şey yap:

AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDialogCustom));

Ve sonra stil istediğin gibi:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AlertDialogCustom" parent="@android:style/Theme.Dialog">
        <item name="android:textColor">#00FF00</item>
        <item name="android:typeface">monospace</item>
        <item name="android:textSize">10sp</item>
    </style>
</resources>

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

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Howard Pinsky

    Howard Pinsk

    6 AĞUSTOS 2006
  • TantalizingTrance

    TantalizingT

    15 ŞUBAT 2009
  • wwjoshdo

    wwjoshdo

    25 Mayıs 2009