Android v21 Theme.Appcompat 颜色重音被忽略,对话框上没有填充

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26608390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:57:44  来源:igfitidea点击:

Android v21 Theme.Appcompat color accent is ignored, no padding on dialogs

androidandroid-5.0-lollipopmaterial-designandroid-support-libraryandroid-appcompat

提问by k1slay

I'm using ActionBarActivityfrom the Android 5 SDK and here is my theme.xmlfor v21

我正在使用Android 5 SDK 中的ActionBarActivity,这是v21 的theme.xml

<style name="AppTheme_Light" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:colorPrimary">@color/abc1</item>
    <item name="android:colorPrimaryDark">@color/abc2</item>
    <item name="android:colorAccent">@color/abc3</item>
</style>

But the colors are ignored, and are replaced by a default teal color and all the dialogs appear without padding.

但是颜色会被忽略,并被默认的青色替换,并且所有对话框都没有填充。

Problem

问题

Also, padding is also ignored in other places like custom toast, problem only occurs in lollipop devices.

此外,在自定义吐司等其他地方也忽略了填充,问题只发生在棒棒糖设备中。

Edit:

编辑:

The padding problem was due to fitsSystemWindowand I got it fixed using
this question..

填充问题是由于fitsSystemWindow,我使用
这个问题修复了它.

But the accent color problem is still there, and it does not just affect dialogs but the whole app.

但是强调色问题仍然存在,它不仅影响对话框,还影响整个应用程序。

回答by Gabriele Mariotti

About the accent color. You are using a AppCompat theme so you should remove Android from the namespace inside your theme.

关于强调色。您正在使用 AppCompat 主题,因此您应该从主题内的命名空间中删除 Android。

<style name="AppTheme_Light" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/abc1</item>
    <item name="colorPrimaryDark">@color/abc2</item>
    <item name="colorAccent">@color/abc3</item>
</style>

About the dialog. AppCompat doesn't support it (as I know).
You can try to use this style in your values-v21folder:

关于对话框。AppCompat 不支持它(据我所知)。
您可以尝试在values-v21文件夹中使用此样式:

<style name="Theme" parent="FrameworkRoot.Theme">
    <item name="android:alertDialogTheme">@style/Theme.AlertDialog</item>
</style>

<style name="Theme.AlertDialog" parent="android:Theme.Material.Light.Dialog.Alert">
    <item name="android:colorPrimary">@color/demo_primary_color</item>
    <item name="android:colorPrimaryDark">@color/demo_colorPrimaryDark</item>
    <item name="android:colorAccent">@color/theme_accent_1</item>
</style>

UPDATE 23/04/2015: SUPPORT LIBRARY V.22.1

23/04/2015 更新:支持库 V.22.1

The new support library v22.1works with the Dialog. You can use an android.support.v7.app.AlertDialogor the new AppCompatDialog.

新的support library v22.1作品与对话框。您可以使用android.support.v7.app.AlertDialog或新的AppCompatDialog

For example:

例如:

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

And use a style like this:

并使用这样的风格:

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

Otherwise you can define in your current theme:

否则,您可以在当前主题中定义:

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <!-- your style -->
    <item name="alertDialogTheme">@style/AppCompatAlertDialogStyle</item>
</style>

and then in your code:

然后在你的代码中:

 import android.support.v7.app.AlertDialog

    AlertDialog.Builder builder =
           new AlertDialog.Builder(this);

回答by Sergey Vakulenko

update

更新

I have applied successfully colors for appCompat dialogs themes , maybe be helpful for someone :

我已经成功地为 appCompat 对话框主题应用了颜色,也许对某人有帮助:

values/style.xml

值/样式.xml

<style name="Theme.MyApp" parent="Theme.AppCompat.Light">

...

/* for android 4 - 4.4, we not define alert dialogs style */

</style>

values-v21/style.xml

值-v21/style.xml

<style name="Theme.MyApp" parent="Theme.AppCompat.Light">

...

/* define alert dialog style for android 5 */
<item name="android:alertDialogTheme">@style/Theme.AlertDialog</item>

</style>

 <style name="Theme.AlertDialog" parent="Theme.AppCompat.Light.Dialog">

    <!--app abar color in Activties Task manager -->
    <item name="colorPrimary">@color/my_color</item>

    <!--copy/paste colors -->
    <item name="colorAccent">@color/my_color</item>

    <!--status bar color -->
    <item name="colorPrimaryDark">@color/my_color</item>


</style>

回答by AlexKorovyansky

Current version of AppCompat doesn't apply colorization to AlertDialogs.

当前版本的 AppCompat 未对 AlertDialogs 应用着色。

Try to use https://github.com/afollestad/material-dialogs, it works great!

尝试使用https://github.com/afollestad/material-dialogs,效果很好!