【问题标题】:Android v21 Theme.Appcompat color accent is ignored, no padding on dialogsAndroid v21 Theme.Appcompat 颜色重音被忽略,对话框上没有填充
【发布时间】:2014-12-23 20:39:57
【问题描述】:

我正在使用 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>

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

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

编辑:

填充问题是由fitsSystemWindow 引起的,我使用
this question. 修复了它。

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

【问题讨论】:

    标签: android android-5.0-lollipop material-design android-support-library android-appcompat


    【解决方案1】:

    关于强调色。您正在使用 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>
    

    关于对话框。 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>
    

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

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

    例如:

    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>
    

    否则你可以在你当前的主题中定义:

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

    然后在你的代码中:

     import android.support.v7.app.AlertDialog
    
        AlertDialog.Builder builder =
               new AlertDialog.Builder(this);
    

    【讨论】:

    • 好的,首先很荣幸cards-lib 的创建者回答了我的问题。填充问题是由fitsSystemWindow 引起的,我使用this question. 修复了它
    • 仅供参考,以防其他人遇到此问题。我遇到了 colorPrimary 的问题。我删除了 android: 它工作正常,否则无法正常工作。其他值有效,不知道为什么这个值有问题。
    • @GabrieleMariotti 请注意,如果您在应用程序主题中定义了alertDialogTheme 属性,则不需要需要将样式传递给AlertDialog.Builder。见here
    • 在 android 4.2.2 中它的不工作对话框和对话框背景看起来不同,为什么?不像棒棒糖对话框
    【解决方案2】:

    更新

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

    值/style.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

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

    【讨论】:

    • 对我来说这不起作用,除非我明确应用它!初始化ProgressDialog时的代码
    【解决方案3】:

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

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

    【讨论】:

    • 感谢您的支持!
    猜你喜欢
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2012-04-24
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多