【问题标题】:Interaction of Themes, background colors, and Action bar主题、背景颜色和操作栏的交互
【发布时间】:2013-04-25 01:42:34
【问题描述】:

我无法理解操作栏外观与主题化之间的交互模式。我的应用设置为使用默认主题,我认为它是黑暗的:

<style name="AppBaseTheme" parent="android:Theme">
</style>

通过应用程序范围的样式从应用程序中删除操作栏会导致主要活动的黑色背景:

    <activity
        android:name="com.atlarge.motionlog.MainActivity"
        android:label="@string/app_name" 
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
    >

没有android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 行,活动背景为白色。如果在activity的onCreate()方法的代码中删除了action bar,action bar也消失了,但是背景仍然是白色的:

    ActionBar actionBar = getActionBar();
    actionBar.hide();       

TL;DR:行为总结:

  • 存在操作栏:白色背景
  • 通过代码移除操作栏:白色背景
  • 通过 XML 移除操作栏:黑色背景

这是为什么呢?有人可以解释(或指向一个好的资源)通过代码与 XML 和背景颜色的操作栏外观的交互吗?

【问题讨论】:

    标签: java android colors themes


    【解决方案1】:

    删除 onCreate 中的操作栏只是隐藏了 ActionBar 视图。 它不会改变主题。

    设置android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 是为您的活动设置一个主题,该主题带有该主题层次结构中的任何继承样式。

    如果您查看 android 源代码中的 themes.xml,您会看到 &lt;style name="Theme"&gt; 样式中的项目 &lt;item name="colorBackground"&gt;@android:color/background_dark&lt;/item&gt;

    然后&lt;style name="Theme.NoTitleBar"&gt;继承Theme的所有样式集&lt;item name="android:windowNoTitle"&gt;true&lt;/item&gt;

    然后&lt;style name="Theme.NoTitleBar.Fullscreen"&gt;继承自NoTitleBarTheme设置&lt;item name="android:windowFullscreen"&gt;true&lt;/item&gt;&lt;item name="android:windowContentOverlay"&gt;@null&lt;/item&gt;

    这解释了为什么在应用该样式时会有深色背景。

    如果您将主题设置为Theme.Light.NoTitleBar.Fullscreen,您将获得相同的效果,但您会从&lt;style name="Theme.Light"&gt; 继承&lt;item name="colorBackground"&gt;@android:color/background_light&lt;/item&gt;,这应该是浅色。

    或者,您可以扩展任何您想要的样式并覆盖任何样式。

    因此,例如,您可以执行类似...

    <style name="MyTheme" parent="@android:style/Theme.NoTitleBar.Fullscreen">
        <item name="colorBackground">@color/my_light_color</item>
        <item name="windowBackground">@drawable/screen_background_selector_light</item>
    </style>
    

    这里有一些资源可以帮助您更深入地理解主题: http://developer.android.com/guide/topics/ui/themes.html http://brainflush.wordpress.com/2009/03/15/understanding-android-themes-and-styles/

    如果你想创建或扩展你自己的主题,总是值得看看 android 源代码,看看你可以扩展和覆盖什么: http://developer.android.com/guide/topics/ui/themes.html#PlatformStyles

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-25
      • 2014-11-22
      相关资源
      最近更新 更多