材料设计中的Alertdialog

4
我只是按照这个http://www.laurivan.com/make-dialogs-obey-your-material-theme/的步骤来设置我的AlertDialog以符合Material Design风格。然而,我发现我仍然无法像这个网站那样进行设置,以下是我的代码和截图:

values-v14/styles.xml:

<!--
    Base application theme for API 14+. This theme completely replaces
    AppBaseTheme from BOTH res/values/styles.xml and
    res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <!-- API 14 theme customizations can go here. -->
</style>

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowActionBarOverlay">true</item>  
    <!-- colorPrimary is used for the default action bar background -->
    <item name="colorPrimary">@color/colorPrimary</item>

    <!-- colorPrimaryDark is used for the status bar -->
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>

    <item name="android:dialogTheme">@style/MyDialogTheme</item>
    <item name="android:alertDialogTheme">@style/MyDialogTheme</item>
</style>

<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:windowBackground">@color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowCloseOnTouchOutside">false</item>
</style>

values/color.xml

<resources>
    <color name="colorPrimaryDark">#3367d6</color>
    <color name="colorPrimary">#4285f4</color>
    <color name="windowBackgroundColor">#eeeeee</color>
    <color name = "transparent">#0000</color>
</resources>

截图: 在此输入图片描述

我想要移除分隔符,使按钮呈现右侧内边距样式,谢谢!


使用这个库 https://github.com/afollestad/material-dialogs - NarenderNishad
我曾经遇到过同样的问题...我认为你没有正确导入AlertDialog...请检查它是否来自android.support.v4.app.DialogFragment包。 - box
2个回答

10

使用新的AppCompat v22.1,您可以使用新的android.support.v7.app.AlertDialog

只需使用以下类似代码:

import android.support.v7.app.AlertDialog

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

并使用这样的样式:

<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="colorAccent">#FFCC00</item>
        <item name="android:textColorPrimary">#FFFFFF</item>
        <item name="android:background">#5fa3d0</item>
    </style>

您可以为所有设备使用一个样式文件。


实际上,在我提出这个问题之前,我已经尝试过你的答案,但它仍然无法正常工作。AlertDialog 将变成“材料风格”+“原始风格”。 - Huang Liang-Syun
你是在所有设备上使用单一的样式吗?还是你正在v-14中覆盖样式呢?链接不是公开的。请检查认证。 - Gabriele Mariotti
链接已经修复,我现在使用单一的风格。 - Huang Liang-Syun
不需要那些Github扩展,它能够惊人地运作。 - Piyush-Ask Any Difference

-1
我发现最好的对话框样式解决方案是在styles.xml中包含属性,同时设置透明颜色(在colors.xml中),确保包含完整的#AARRGGBB。
<color name="transparent">#00000000</color>

然后还要确保将主题添加到对话框布局中:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme">
        <TextView
            android:id="@+id/dialog_info"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:layout_marginBottom="4dp"
            android:text="Dialog Text"/>
    </LinearLayout>

注意:您应该将“MyDialogTheme”的父级设置为“Theme.AppCompat.Light.Dialog”的父级(在您的示例中)

按照开发人员指南中的“将事件传递回对话框的主机”部分,链接在此处:Link

确保在对话框片段的类中覆盖onAttach()和onCreateDialog()。

请注意,您不必使主活动扩展FragmentActivity,您可以扩展AppCompatActivity并仍然在活动类中包含库(适用于API 11+):

android.app.DialogFragment而不是android.support.v4.app.DialogFragment

尽管您的屏幕截图显示日期选择器对话框,但上述内容适用于简单(和最常用的)警报和/或对话框。在我上面提供的文档链接中,他们提供了更多有关日期和时间选择器的特定信息。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接