代码之家  ›  专栏  ›  技术社区  ›  Steve Haley

警报对话框中的按钮样式

  •  17
  • Steve Haley  · 技术社区  · 16 年前

    有人知道如何覆盖AlertDialog按钮的默认样式吗?我在Android源代码中查找了 themes styles 尝试了不同的东西,但我还没找到一个可行的方法。

    我下面的东西可以用来改变背景,但对按钮没有任何作用。 myTheme 适用于整体 <application> 通过舱单(为清晰起见,删除了其他一些项目,但它们仅与标题栏相关。)

    <style name="myTheme" parent="android:Theme">
        <item name="android:buttonStyle">@style/customButtonStyle</item>
        <item name="android:alertDialogStyle">@style/dialogAlertTheme</item>
    </style>
    
    <style name="dialogAlertTheme" parent="@android:style/Theme.Dialog.Alert">
        <item name="android:fullDark">@drawable/dialog_loading_background</item>
        <item name="android:topDark">@drawable/dialog_alert_top</item>
        <item name="android:centerDark">@drawable/dialog_alert_center</item>
        <item name="android:bottomDark">@drawable/dialog_alert_bottom</item>
        <!-- this last line makes no difference to the buttons -->
        <item name="android:buttonStyle">@style/customButtonStyle</item> 
    </style>
    

    6 回复  |  直到 16 年前
        1
  •  1
  •   MrSnowflake    7 年前

    不正确:

    我想你必须实现你自己的目标 Dialog 班级。

        2
  •  30
  •   class stacker    13 年前

    鉴于斯诺弗雷克先生的推理不准确,我冒昧地提供另一个答案。

    有些东西 错误的 标记 在Steve Haley的问题中,它混淆了alertDialogStyle和alertDialogTheme,很可能是因为alertDialogTheme是在alertDialogStyle出现很久之后引入的。

    因此,即使android平台上有@android:style/Theme.Dialog.Alert,除非android平台支持

    在问题的标记中,父级=“@android:style/Theme.Dialog.Alert” 什么也做不了

    这就是标记的样子;并非所有Android版本都支持所有功能。

    <style name="myTheme" parent="android:Theme">
        <item name="android:buttonStyle">@style/customButtonStyle</item>
        <item name="android:alertDialogStyle">@style/dialogAlertStyle</item>
        <item name="android:alertDialogTheme">@style/dialogAlertTheme</item>
    </style>
    
    <style name="dialogAlertStyle" parent="@android:style/AlertDialog">
        <item name="android:fullDark">[...]</item>
        [...]
    </style>
    
    <style name="dialogAlertTheme" parent="@android:style/Theme.Dialog.Alert">
        <item name="android:windowBackground">[...]</item>
        [...]
    </style>
    

    定制警报对话框样式已经有相当长的时间了,但仅限于为“fullDark”、“topDark”等提供(背景)绘图。

    自定义警报对话框主题将打开一个方法来提供属性,如windowBackground、windowTitleStyle等,但如前所述,您需要一个Android版本,该版本支持主题的alertDialogThem属性/项。我不知道这是什么时候推出的,但它还没有安卓2.2和Eclipse会告诉你无论如何。。。

    我没有足够的资源来验证Snowflake先生的结论,即不可能在XML中设置警报对话框按钮的样式,但是除非我们面对Android的某些令人讨厌的方面,其中一个功能确实缺失,否则我觉得这不太可能。

    事实上,问题中缺少的是这方面最相关的部分,即

    <style name="customButtonStyle" />
    

    综合结论:

        3
  •  11
  •   Witoldio    10 年前

    试试这个:

     <item name="buttonBarStyle">@style/Widget.AppCompat.ButtonBar</item>
        <item name="buttonBarButtonStyle">@style/AppTheme.Button</item>
        <item name="buttonBarPositiveButtonStyle">@style/YoursTheme</item>
        <item name="buttonBarNegativeButtonStyle">@style/YoursTheme</item>
        <item name="buttonBarNeutralButtonStyle">@style/YoursTheme</item>
    
        4
  •  6
  •   miracula    12 年前

    您可以重写“buttonBarButtonStyle”而不是“buttonStyle”,如下所示:

    <style name="dialogAlertTheme" parent="@android:style/Theme.Dialog.Alert">
      <item name="android:buttonBarButtonStyle">@style/customButtonStyle</item>
    </style>
    
        5
  •  4
  •   Peter Keefe    7 年前

    如果使用的是材质组件:

    <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:alertDialogTheme">@style/AlertDialogTheme.Light</item>
    </style>
    
    <style name="AlertDialogTheme.Light" parent="ThemeOverlay.MaterialComponents.Dialog.Alert" >
        <item name="android:buttonBarButtonStyle">@style/InsertCustomButtonStyleHere</item>
    </style>
    

    这将保持你的主题的颜色,但取代按钮的样式,例如匹配 Widget.MaterialComponents.Button.TextButton .

        6
  •  2
  •   Gustavo Baiocchi Costa    11 年前

    我做了这样一个方法,将改变默认按钮的颜色和背景

    public static void setDefaultColorTextForDialogButton(AlertDialog alertDialog, Resources r) {
        Button b;
    
        b= alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
        if(b != null) {
            b.setTextColor(r.getColor(R.color.default_text));
            b.setBackgroundColor(Color.WHITE);
        }
        b = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
        if(b != null) {
            b.setTextColor(r.getColor(R.color.default_text));
            b.setBackgroundColor(Color.WHITE);
        }
        b = alertDialog.getButton(DialogInterface.BUTTON_NEUTRAL);
        if(b != null) {
            b.setTextColor(r.getColor(R.color.default_text));
            b.setBackgroundColor(Color.WHITE);
        }
    }
    

    这可能对某些人有用!!!

        7
  •  0
  •   portsample    5 年前

    这使我可以自定义AlertDialog按钮和布局。

    private void openLookupTableEditDialog() {
        AlertDialog.Builder openLookupTableEditDialog = new AlertDialog.Builder(this);
        View v = this.getLayoutInflater().inflate(R.layout.lookup_table_edit_dialog, null);
    
        openLookupTableEditDialog.setView(v);
        final AlertDialog alert = openLookupTableEditDialog.create();
        openLookupTableEditDialog.setTitle("Lookup Table Edit");
        Button btnSpecies = v.findViewById(R.id.btnSpeciesLookupEdit);
        Button btnLocation = v.findViewById(R.id.btnLocationLookupEdit);
        Button btnSampler = v.findViewById(R.id.btnSamplerLookupEdit);
        Button btnExit = v.findViewById(R.id.btnCloseLookupTableEdit);
        alert.setView(v);
        alert.show();
    
        btnSpecies.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                editSpeciesTable();
                alert.dismiss();
            }
        });
        btnSampler.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                editSamplerTable();
                alert.dismiss();
            }
        });
        btnLocation.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                editLocationTable();
                alert.dismiss();
            }
        });
        btnExit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
               alert.dismiss();
            }
        });
        ...
    }