【问题标题】:Custom theme for AlertDialog not workingAlertDialog 的自定义主题不起作用
【发布时间】:2016-06-20 07:08:15
【问题描述】:

我正在尝试自定义 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 没有具有相同文本颜色的按钮。文本颜色类似于绿色。似乎它是从系统继承颜色而不是自定义主题。 这是图片:

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();

编辑2:

我还尝试更改对话框的强调色,但我没有看到该颜色:

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

即使这样也不会改变按钮文本的颜色 :(.

【问题讨论】:

  • 为什么你使用CustomPopupBuilder而不是你可以使用默认的AlertDialog..!!
  • @jankigadhiya 我使用了 AlertDialog 但结果相同。它没有根据我的主题/样式自定义 alertDialog。

标签: android android-alertdialog android-theme


【解决方案1】:

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

更改导入行
import android.app.AlertDialog

到

import android.support.v7.app.AlertDialog

帮了我。

2019 年更新:

由于 google 发布了 AndroidX 库,新的答案将是

import androidx.appcompat.app.AlertDialog;

感谢@ChristosThemelis

【讨论】:

  • 是的,如果你要使用 appcompat 主题,你必须使用 supper.v7 的东西
  • 如果您使用 AndroidX 库,请使用 import androidx.appcompat.app.AlertDialog;
  • 我确实从import androidx.appcompat.app.AlertDialog; 导入了AlertDialog,但我仍然看不到对话框中的任何更改。我使用了 Material Design 官方网站中给出的对话框主题,这里是链接 - material.io/develop/android/components/dialogs@ChristosThemelis
【解决方案2】:

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

在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();

...对话框应该有强调颜色和所有设置为您在@style/YourDialogTheme 中指定的内容

【讨论】:

    【解决方案3】:

    创建如下所示的新样式:

    <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();
    

    【讨论】:

      【解决方案4】:

      如果您的主题没有反映您的 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));
      

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

      【讨论】:

        【解决方案5】:

        举个例子希望对你有帮助

        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;
            }
        
            }
        

        XML 布局

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#00000000"
        >
        
        <RelativeLayout
            android:id="@+id/rlmain"
            android:layout_width="fill_parent"
            android:layout_height="150dip"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:background="#569CE3" >
        
            <RelativeLayout
                android:id="@+id/relativeLayout1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginLeft="25dip"
                android:layout_marginTop="10dip" >
        
                <TextView
                    android:id="@+id/title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:text="Are you Sure?"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="#ffffff"
                    android:textSize="13dip" />
            </RelativeLayout>
        
            <RelativeLayout
                android:id="@+id/relativeLayout2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/relativeLayout1"
                android:layout_alignRight="@+id/relativeLayout1"
                android:layout_below="@+id/relativeLayout1"
                android:layout_marginTop="5dip" >
            </RelativeLayout>
        
            <ProgressBar
                android:id="@+id/process"
                style="?android:attr/progressBarStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:layout_marginRight="3dip"
                android:layout_marginTop="3dip" />
        
            <RelativeLayout
                android:id="@+id/relativeLayout3"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/relativeLayout2"
                android:layout_below="@+id/relativeLayout2"
                android:layout_toLeftOf="@+id/process" >
        
                <TextView
                    android:id="@+id/message"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:text="Medium Text"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="#ffffff"
                    android:textSize="13dip"/>
        
            </RelativeLayout>
        
            <Button
                android:id="@+id/next_button"
                android:layout_width="90dip"
                android:layout_height="35dip"
                android:layout_alignParentBottom="true"
                android:textColor="@drawable/button_text_color"
                 android:background="@drawable/blue_button"
                 android:layout_marginBottom="5dp"
                   android:textSize="10dp"
        
                android:layout_alignRight="@+id/relativeLayout3"
                android:text="Okay" />
        
            <Button
                android:id="@+id/button2"
                android:text="Cancel"
                android:textColor="@drawable/button_text_color"
                android:layout_width="90dip"
                android:layout_height="35dip"
                android:layout_marginBottom="5dp"
                 android:background="@drawable/blue_button"
                 android:layout_marginRight="7dp"
                android:textSize="10dp"
                android:layout_alignParentBottom="true"
                android:layout_toLeftOf="@+id/next_button"
                 />
        
        </RelativeLayout>
        

        【讨论】:

          【解决方案6】:

          我能够在我的用例中解决这个问题,方法是使警报对话框的样式的父级与我的主样式的父级相同。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-08-12
            • 2011-10-15
            • 1970-01-01
            • 2016-06-15
            • 1970-01-01
            • 2023-03-15
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多