如何自动将材质主色和强调色应用于对话框

Dal*_*kar 1 java android material-design

我有自定义主题与更改的主要和强调颜色.但是AlertDialog当我使用以下方法创建对话框时,不会自动拾取这些颜色:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
Run Code Online (Sandbox Code Playgroud)

这就是我得到的 - 具有默认颜色的对话框:

默认主题

这就是我想要的 - 与自定义颜色对话:

在此输入图像描述

我可以创建自定义AlertDialog主题并在AlertDialog创建过程中应用它:

AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AppTheme_Dialog);
Run Code Online (Sandbox Code Playgroud)

我想跳过使用theme参数调用构造函数.

有没有办法强制所有主题上的自定义颜色,而不是在样式中导出它们,包括AlertDialog基本主题?

我必须支持的Minimup API级别是15.


我的活动代码

import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dialog();
    }

    public void dialog()
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Dialog");
        builder.setMessage("Lorem ipsum dolor ...");
        builder.setPositiveButton("OK", null);
        builder.setNegativeButton("Cancel", null);
        builder.show();
    }
}
Run Code Online (Sandbox Code Playgroud)

我的定制风格

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.Dialog" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

我的颜色

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
</resources>
Run Code Online (Sandbox Code Playgroud)

Shi*_*dam 5

在App主题中添加:

<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- your style -->
<item name="alertDialogTheme">@style/AppTheme.Dialog</item>
Run Code Online (Sandbox Code Playgroud)

然后,

import android.support.v7.app.AlertDialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
Run Code Online (Sandbox Code Playgroud)