Androidの個別にカスタムダイアログでのスタイルの設定をする

Dialogって頻繁に使うのですが各場面で共通の場合はテーマでやっちぇばばいいけど個別に設定するにはどうしたらいいか。メモ。

1.XMLでレイアウトを定義する。背景画面を使う場合は9patchで作成。

  • 例:layout/custom_dialog.xml

2.スタイルをXMLで定義し、res/values/以下に設置(下部参照)

  • ファイル名はthemes_xxx.xml
  • styleタグのparent属性に”android:style/Theme.Dialog”にセット

3.Dialogクラスを継承したカスタムクラスを作成(下部参照)

  • コンストラクタに2で作成したスタイルをカスタムリソースで指定

/////////themes_xxx.xml/////////
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!– タイトルなし、文字色及び、背景指定 –>
    <style name="Theme_CustomDialog" parent="android:style/Theme.Dialog">
        <item name="android:textColor">#333333</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">画像または色指定</item>
    </style>
</resources>  

//////////Customダイアログクラス////////
public class CustomDialog extends Dialog {

    /**
      コンストラクタ  
     
@param context
     */
    public CustomProgressDialog(Context context) {
        //スタイルの指定
        super(context, R.style.Theme_CustomDialog);
        //レイアウト指定
        setContentView(R.layout.custom_dialog.xml);        
    }

}