2011-09-01 13 views
18

私は、ユーザーが好みの色に背景色を(好みによって)変更できる主なアクティビティを持っています。私の問題は、カスタムダイアログの背景色を変更できないことです。スタックオーバーフローでダイアログの背景色をプログラム的に変更するにはどうすればよいですか?

他の答えが示唆:好ましい色に

(A)overiding the default theme。実行時にテーマを変更することはお勧めできませんので、私はこの場合適切な解決策ではないと思います。

(b)のChanging in styles.xml(私は実行時に変更することはできませんこの場合に適していない)

(C)Overriding the AlertBuilder class(これは全体の警告ダイアログを濃淡)

私が来ている。この最も近いです色を変更することはアラートビルダーのタイトルを削除し、カスタムビューの背景をお気に入りの色(eg.pink)に設定することです。残念ながら、これはダイアログの上部と下部に醜いストリップを与えます。

コードが画像の後に含まれていますので、ダイアログの背景を変更する方法についてのご意見をお待ちしております。

Dialog appearance

カスタムダイアログビューの背景色を変更するためのデフォルトの外観

protected Dialog onCreateDialog(int dialogId) { 
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    final AlertDialog.Builder builder = new AlertDialog.Builder(this); 


     final View viewMessEdit = inflater.inflate(R.layout.example,(ViewGroup) findViewById(R.id.dialog_mess_edit_root)); 
       builder.setView(viewMessEdit); 
     builder.setTitle("Alert builder's title"); 
} 

コードのコード

(およびアラートビルダーのタイトルが削除されます)
protected Dialog onCreateDialog(int dialogId) { 
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    final AlertDialog.Builder builder = new AlertDialog.Builder(this); 


     final View viewMessEdit = inflater.inflate(R.layout.example,(ViewGroup) findViewById(R.id.dialog_mess_edit_root)); 
       builder.setView(viewMessEdit); 
       viewMessEdit.setBackgroundResource(R.color.pink_dark); 

} 
+0

あなたは写真としてこのダイアログを終了できますか?はいの場合は共有できますか? – pengwang

答えて

8

私はちょっと同じ問題に直面しました。それを解決する唯一の方法は、自分のバージョンのレイアウトを拡張することでした。あなたのケースでは、それがAlertDialogであることがわかります。私がお勧めするのは、カスタマイズされたAlertDialogである一意のクラスを作成し、これに対するレイアウトを作成してからこれを膨張させることです。

私は多くの助けをしたポストです。 http://blog.androgames.net/10/custom-android-dialog/

私はこのポストに続き、私の問題をダイアログのカスタマイズに解決しました。

ご不明な点がございましたら、お知らせください。

ありがとうございました。

+1

androgamesの投稿へのリンクありがとうございます。それは明らかに書かれ、よくコメントされていた。ここに関連するブログ記事があります。 http://dtmilano.blogspot.com/2010/01/android-ui-colored-dialogs.html – Mel

+15

@rogcgリンクが機能しません。 – Deepak

+0

私はWayBackマシンを使ってキャッシュされたバージョンへのリンクを更新しました。 – kabuko

17

私は1つのルール解決策を見つけました!

d.getWindow().setBackgroundDrawableResource(R.drawable.menubackground); 

通常のダイアログで私のために働きます。 しかし、それがAlertDialogで動作するかどうかはわかりません。

+2

私はそれを試しましたが、それはしません。私は無地で試してみました。バックグラウンドにalertdialogの影の代わりに私の色が現れました。 – soger

+0

@ user3696814 AlertDialogでうまくいきます。 – CopsOnRoad

3

カスタムレイアウトを作成せずにハックを見つけました。AlertDialogのいくつかのプロパティで再生して複数のデザインを作成できます。

何をする必要があります:AlertDialogが画面に表示されている場合は、OnShowListener

と呼ばれています。したがって、dialog.setOnShowListener(this)を追加すると、AlertDialogをカスタマイズすることができます。

コード:

// Create AlertDialog 
AlertDialog.Builder adb = new AlertDialog.Builder(context1); 
    adb.setTitle(context1.getString(R.string.app_name)) 
    .setMessage(message) 
    .setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 

     } 
}); 
AlertDialog dialog = adb.create(); 

// Make some UI changes for AlertDialog 
dialog.setOnShowListener(new DialogInterface.OnShowListener() { 
    @Override 
    public void onShow(final DialogInterface dialog) { 

     // Add or create your own background drawable for AlertDialog window 
     Window view = ((AlertDialog)dialog).getWindow(); 
     view.setBackgroundDrawableResource(R.drawable.your_drawable); 

     // Customize POSITIVE, NEGATIVE and NEUTRAL buttons. 
     Button positiveButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_POSITIVE); 
     positiveButton.setTextColor(context1.getResources().getColor(R.color.primaryColor)); 
     positiveButton.setTypeface(Typeface.DEFAULT_BOLD); 
     positiveButton.invalidate(); 

     Button negativeButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_NEGATIVE); 
     negativeButton.setTextColor(context1.getResources().getColor(R.color.primaryColor)); 
     negativeButton.setTypeface(Typeface.DEFAULT_BOLD); 
     negativeButton.invalidate(); 

     Button neutralButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_NEUTRAL); 
     neutralButton.setTextColor(context1.getResources().getColor(R.color.primaryColor)); 
     neutralButton.setTypeface(Typeface.DEFAULT_BOLD); 
     neutralButton.invalidate(); 
    } 
}); 
+0

のような魅力)thx –

1

提案ソリューション

d.getWindow().setBackgroundDrawableResource(R.drawable.menubackground); 

それはここでは、ダイアログの影に

を変更し、私であるため(6.0でテスト)はうまく動作しません。ソリューション:

public static void changeDialogBackground(final Dialog dialog,final int resId) { 
    dialog.getWindow().getDecorView().setBackgroundResource(resId); 
    dialog.setOnShowListener(new DialogInterface.OnShowListener() { 
     @Override 
     public void onShow(final DialogInterface dialog) { 
      Window window = ((AlertDialog)dialog).getWindow(); 
      window.getDecorView().setBackgroundResource(resId); 
     } 
    }); 
} 
3

対話背景色

dialog.getWindow().setBackgroundDrawableResource(R.color.glassblack); 

は本当に私のためにうまく働きました。

関連する問題