web-dev-qa-db-ja.com

appCompat 22.1以降の新しいAlertDialogの使い方とスタイル

デフォルトのAndroid AlertDialogからappCompat-22.1に含まれる新しいものに移行しようとしています。今のところ、Android.support.v7.app.AlertDialogパッケージをインポートするだけでいいのです。

しかしどのようにしてそれをスタイルすることができますか?たとえば、ボタンのプラス/マイナスの色、タイトルの色、メッセージの色、背景色を変更できますか。

144
ThanosFisherman

AlertDialogを作成するときに、使用するテーマを設定できます。

例 - ダイアログの作成

AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyAlertDialogStyle);
builder.setTitle("AppCompatDialog");
builder.setMessage("Lorem ipsum dolor...");
builder.setPositiveButton("OK", null);
builder.setNegativeButton("Cancel", null);
builder.show();

styles.xml - カスタムスタイル

<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <!-- Used for the buttons -->
    <item name="colorAccent">#FFC107</item>
    <!-- Used for the title and text -->
    <item name="Android:textColorPrimary">#FFFFFF</item>
    <!-- Used for the background -->
    <item name="Android:background">#4CAF50</item>
</style>

結果

styled alertdialog

編集

タイトルの外観を変更するには、次の操作を行います。まず新しいスタイルを追加します。

<style name="MyTitleTextStyle">
    <item name="Android:textColor">#FFEB3B</item>
    <item name="Android:textAppearance">@style/TextAppearance.AppCompat.Title</item>
</style>

その後、単にあなたのMyAlertDialogStyleでこのスタイルを参照してください。

<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    ...
    <item name="Android:windowTitleStyle">@style/MyTitleTextStyle</item>
</style>

このようにして、Android:textColorPrimaryを介してメッセージに対して異なるtextColorを、そしてスタイルを介してタイトルに対して異なるを定義することができます。

429
reVerse

すべてのアプリケーションにテーマを使用し、Dialogのスタイル設定に2番目のパラメータを使用しない

<style name="MyTheme" parent="Base.Theme.AppCompat.Light">
    <item name="alertDialogTheme">@style/dialog</item>
    <item name="colorAccent">@color/accent</item>
</style>

<style name="dialog" parent="Base.Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">@color/accent</item>
</style>

テーマでカラーアクセントを使用している私のアプリでは、テーマcolorAccentでalertDialogのボタンを表示しないでください。テーマでダイアログスタイルを追加する必要があります。

56
neoteknic

新しいAndroid.support.v7.app.AlertDialogを使用し、ボタンに異なる色を使用し、カスタムレイアウトを使用する場合は、私の https:// Gistをご覧ください。 github.com/JoachimR/6bfbc175d5c8116d411e

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.custom_layout, null);

    initDialogUi(v);

    final AlertDialog d = new AlertDialog.Builder(activity, R.style.AppCompatAlertDialogStyle)
            .setTitle(getString(R.string.some_dialog_title))
            .setCancelable(true)
            .setPositiveButton(activity.getString(R.string.some_dialog_title_btn_positive),
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            doSomething();
                            dismiss();
                        }
                    })
            .setNegativeButton(activity.getString(R.string.some_dialog_title_btn_negative),
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dismiss();
                        }
                    })
            .setView(v)
            .create();

    // change color of positive button         
    d.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            Button b = d.getButton(DialogInterface.BUTTON_POSITIVE);
            b.setTextColor(getResources().getColor(R.color.colorPrimary));
        }
    });

    return d;
}

enter image description here

18
IHeartAndroid

@reVerseの回答に従ってください。ただし、私の場合は、AppThemeのように既にプロパティがいくつかあります。

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    ...
    <item name="Android:textColor">#111</item>
    <item name="Android:textSize">13sp</item>
</style>

だから私の対話は次のようになります
enter image description here

私はそれを解決しました

1)インポートをAndroid.app.AlertDialogからAndroid.support.v7.app.AlertDialogに変更します
2)AppThemeの2つのプロパティをnull値で上書きします

<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <!-- Used for the buttons -->
    <item name="colorAccent">#FFC107</item>
    <!-- Used for the title and text -->
    <item name="Android:textColorPrimary">#FFFFFF</item>
    <!-- Used for the background -->
    <item name="Android:background">#4CAF50</item>


    <item name="Android:textColor">@null</item>
    <item name="Android:textSize">@null</item>
</style>

AlertDialog.Builder builder = new AlertDialog.Builder(mContext, R.style.MyAlertDialogStyle);

それが他の人を助けることを願っています

enter image description here

6
Phan Van Linh