【问题标题】:Android prevent activity style from being applied to alertdialogsAndroid阻止将活动样式应用于警报对话框
【发布时间】:2013-07-25 23:52:44
【问题描述】:

我的 style.xml 中有一些小自定义:

<style name="AppBaseTheme" parent="android:Theme.Holo.Light.NoActionBar"></style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:background">@color/light_grey</item>
        <item name="android:textColor">@color/white</item>
    </style>

现在样式已正确应用于我的活动。

但是当我创建一个警报对话框时,背景颜色会应用于对话框的标题和正文,这是我不想要的。我希望 alertdialog 保持其股票样式。

这是警报对话框:

AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle("Wtitle").setMessage("message");
            builder.setNeutralButton("ok", null);
            builder.show();

谁能帮忙?

【问题讨论】:

  • 您是否将 AppBaseTheme 应用于应用程序,而不是活动?

标签: android styles android-alertdialog


【解决方案1】:

有一个可行的解决方案 - 使用另一个 AlertDialog.Builder 构造函数,其主题如the documentation 中所述,想法基本上来自这个"How to change theme for AlertDialog":

  • 您的styles.xml 中有一件奇怪的事情:应用主题定义android:background 而不是android:windowBackground。似乎没有理由这样做,因为如果 all 视图需要相同的背景(我怀疑这是可能的),那么您可以为视图设置一些基本主题。我认为,干扰应用程序主题和视图主题基本上不是一个好主意,因为应用程序需要完全不同的属性。所以,让我们如下:

    <resources>
        <style name="AppBaseTheme" parent="android:Theme.Holo.Light.NoActionBar"></style>
    
        <!-- Application theme. -->
        <style name="AppTheme" parent="AppBaseTheme">
            <item name="android:windowBackground">@color/light_grey</item>
            <item name="android:textColor">@color/white</item>
        </style>
    </resources>
    
  • Dialog Builder 应该使用它自己的主题来创建:

    AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, android.R.style.Theme_Dialog));
    

    这里请注意,由于某种原因,构造函数AlertDialog.Builder (Context context, int theme) 做得不对,ContextThemeWrapper 是必要的(似乎是因为并非所有属性都在主题中,并且主题仅使用 ContextThemeWrapper 才能“重新创建”)。

【讨论】:

    猜你喜欢
    • 2020-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多