AlertDialog的自定义主题无法正常工作

时间:2016-06-20 07:08:16

标签: android android-alertdialog android-theme

我正在尝试为AlertDialog按钮自定义重音颜色。但它似乎没有任何影响它似乎继承了系统的颜色。这是我的风格/主题。

   <color name="actionable_items">#0574ac</color>  <!-- it is blue color --> 
   <style name="LLDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
      <!--buttons color-->
      <item name="colorAccent">@color/actionable_items</item>
      <!--item RadioButton or CheckBox color-->
      <item name="colorControlActivated">@color/actionable_items</item>
      <item name="colorPrimary">@color/actionable_items</item>
      <item name="colorPrimaryDark">@color/actionable_items</item>
      <item name="android:listChoiceIndicatorMultiple">@color/actionable_items</item>
      <item name="android:listChoiceIndicatorSingle">@color/actionable_items</item>
   </style>

这是我的代码,它正在尝试构建Alertdialog。

final CustomPopupBuilder removePlaceDialog =  new CustomPopupBuilder(new ContextThemeWrapper(context,
                                                            R.style.LLDialog));
            removePlaceDialog.setTitle(getString(R.string.delete_place, placeName));
            removePlaceDialog.setMessage(getString(R.string.delete_place_message));
            removePlaceDialog.setPositiveButton(R.string.ok_button, new DialogInterface.OnClickListener() {

               public void onClick(DialogInterface dialog, int which) {
                  ....
                  ....
               }
            });
            removePlaceDialog.setNegativeButton(R.string.cancel, null);
            removePlaceDialog.create().show();

最终的AlertDialog没有具有相同文字颜色的按钮。文字颜色与绿色相似。它似乎是从系统继承颜色而不是自定义主题。 这是图像:enter image description here

EDIT1

我尝试使用AlertDialog.Builder,但它给了我相同的结果。

final AlertDialog.Builder removePlaceDialog =  AlertDialog.Builder(new ContextThemeWrapper(context,
                                                             R.style.LLDialog));
                removePlaceDialog.setTitle(getString(R.string.delete_place, placeName));
                removePlaceDialog.setMessage(getString(R.string.delete_place_message));
                removePlaceDialog.setPositiveButton(R.string.ok_button, new DialogInterface.OnClickListener() {

                   public void onClick(DialogInterface dialog, int which) {
                      ....
                      ....
                   }
                });
                removePlaceDialog.setNegativeButton(R.string.cancel, null);
                removePlaceDialog.create().show();

EDIT2:

我也尝试更改对话框的强调色,但我看不到那种颜色:

<style name="LLDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
      <!--buttons color-->
      <item name="colorAccent">#990000</item>
      ...
      ...
</style>

即使这样也不会改变按钮文字颜色:(。

6 个答案:

答案 0 :(得分:8)

请检查您的AlertDialog导入。它应该从v7支持lib导入,以便在较旧的Android版本上应用样式。我有同样的问题,并从

更改导入行
import android.app.AlertDialog

import android.support.v7.app.AlertDialog

帮助了我。

答案 1 :(得分:2)

创建如下所示的新样式:

<style name="AlertDialogCustom" parent="@android:style/Theme.Holo.Light.Dialog">        
  <!--buttons color-->
  <item name="colorAccent">@color/actionable_items</item>
  <!--item RadioButton or CheckBox color-->
  <item name="colorControlActivated">@color/actionable_items</item>
  <item name="colorPrimary">@color/actionable_items</item>
  <item name="colorPrimaryDark">@color/actionable_items</item>
  <item name="android:listChoiceIndicatorMultiple">@color/actionable_items</item>
  <item name="android:listChoiceIndicatorSingle">@color/actionable_items</item>
</style>

然后在你的班上:

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.AlertDialogCustom));    
AlertDialog alertDialog = dialogBuilder.create();    
alertDialog.show();

答案 2 :(得分:0)

这里有一个例子希望它可以帮到你

self.hasChildren = ko.pureComputed(function() {
  return self.children().length;
});

XML布局

public class CustomDialogUI {
    Dialog dialog;
    Vibrator vib;
    RelativeLayout rl;

    @SuppressWarnings("static-access")
    public void dialog(final Context context, String title, String message,
            final Runnable task) {
        dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.custom);
        dialog.setCancelable(false);
        TextView m = (TextView) dialog.findViewById(R.id.message);
        TextView t = (TextView) dialog.findViewById(R.id.title);
        final Button n = (Button) dialog.findViewById(R.id.button2);
        final Button p = (Button) dialog.findViewById(R.id.next_button);
        rl = (RelativeLayout) dialog.findViewById(R.id.rlmain);
        t.setText(bold(title));
        m.setText(message);
        dialog.show();
        n.setText(bold("Close"));
        p.setText(bold("Ok"));
        // color(context,rl);
        vib = (Vibrator) context.getSystemService(context.VIBRATOR_SERVICE);
        n.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                vib.vibrate(15);
                dialog.dismiss();
            }
        });
        p.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                vib.vibrate(20);
                dialog.dismiss();
                task.run();
            }
        });
    }
     //customize text style bold italic....
    public SpannableString bold(String s) {
        SpannableString spanString = new SpannableString(s);
        spanString.setSpan(new StyleSpan(Typeface.BOLD), 0,
                spanString.length(), 0);
        spanString.setSpan(new UnderlineSpan(), 0, spanString.length(), 0);
        // spanString.setSpan(new StyleSpan(Typeface.ITALIC), 0,
        // spanString.length(), 0);
        return spanString;
    }

    }

答案 3 :(得分:0)

如果您的主题没有反映您的AlertDialog按钮颜色,您也可以通过编程方式进行管理,这可能很有用。

    AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AppCompatAlertDialogStyle));
    builder.setCancelable(false);

    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
    String message ="message";
    builder.setTitle("title");

    builder.setMessage(message);
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });
    //   builder.setNegativeButton("No, Thanks", null);
    //builder.show();
    AlertDialog alert = builder.create();
    alert.show();
    Button pbutton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
    pbutton.setTextColor(getResources().getColor(R.color.colorAccent));
    Button nbutton = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
    nbutton.setTextColor(getResources().getColor(R.color.accent));

如果您需要,可以在这里更改两个按钮的颜色,希望它可能对您有用

答案 4 :(得分:0)

我遇到了同样的问题,这就是我解决它的方法:

在styles.xml中,声明主题并设置属性android:alertDialogTheme

<style name="YourTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- your theme attributes here -->
    <item name="android:alertDialogTheme">@style/YourDialogTheme</item>
</style>

<style name="YourDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert" >
    <!-- your dialog-theme attributes here -->
</style>

现在,如果你像这样显示一个AlertDialog ......

AlertDialog.Builder builder = new AlertDialog.Builder(activity); //where activity is an Activity with the theme attribute set to android:theme="@style/YourTheme" (in AndroidManifest)
//...
builder.show();

...对话框应该有accentColor,并且所有内容都设置为你在@ style / YourDialogTheme中指定的内容

答案 5 :(得分:0)

我能够通过使警报对话框的样式的父代与主样式的父代相同来解决此用例。