【问题标题】:How to style buttons in Alert Dialog Fragment using Material Design?如何使用 Material Design 在警报对话框片段中设置按钮样式?
【发布时间】:2014-10-17 09:45:34
【问题描述】:

我想更改警报对话框上的默认按钮样式。

标准的警报对话框片段(在 Android L 中)如下所示:

我希望将右键设置为普通按钮而不是无边框按钮。 Google 本身似乎在各种对话框中使用了这种模式,例如:

有谁知道这是否可能,无需从头开始重新创建整个对话框?

【问题讨论】:

标签: android dialog android-alertdialog material-design


【解决方案1】:

您可以使用以下属性为主题中的按钮设置样式:android:buttonBarPositiveButtonStyleandroid:buttonBarNegativeButtonStyleandroid:buttonBarNeutralButtonStyle

【讨论】:

    【解决方案2】:

    好的,通过更新到(刚刚发布的)版本 21 自动解决了这个问题。现在按钮自动变为原色 :-)

    编辑:它们不是原色,而是 Android 的基本绿松石色..

    【讨论】:

    【解决方案3】:

    此解决方案用于在AppCompatDialogFragment 中使用材质效果更改按钮颜色。

    <android.support.v7.widget.AppCompatButton
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="20dp"
    android:text="@string/buttin"
    android:theme="@style/AppTheme.MyButton"
    />
    

    样式文件 - v21

    <style name="AppTheme.MyButton" parent="Theme.AppCompat.Dialog.Alert">
      <item name="android:colorButtonNormal">@color/button_color</item>
      <item name="android:textColor">@color/text_color</item>
    </style>
    

    样式文件

    <style name="AppTheme.MyButton" parent="Theme.AppCompat.Dialog.Alert">
      <item name="colorButtonNormal">@color/button_color</item>
      <item name="android:textColor">@color/text_color</item>
    </style>
    

    【讨论】:

      【解决方案4】:

      科特林

      1. 在您应用的主题/样式中,添加以下行:

        <item name="android:buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
        <item name="android:buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
        <item name="android:buttonBarNeutralButtonStyle">@style/NeutralButtonStyle</item>
        
      2. 然后添加以下样式:

        <style name="NegativeButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
            <item name="android:textColor">@color/red</item>
        </style>
        
        <style name="PositiveButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
            <item name="android:textColor">@color/red</item>
        </style>
        
        <style name="NeutralButtonStyle" 
        parent="Widget.MaterialComponents.Button.TextButton.Dialog">
            <item name="android:textColor">#00f</item>
        </style>
        

      使用此方法无需在 AlertDialog 构建器中设置主题。

      【讨论】:

        猜你喜欢
        • 2014-12-14
        • 2011-02-01
        • 1970-01-01
        • 2020-12-30
        • 2014-12-08
        • 2021-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多